blob: 2aa6e321d31d29e39ac526f73ea525d6b7159dcc [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Jamie Gennisb9cbdae2014-10-02 18:36:13 -070015package proptools
16
17import (
Colin Cross6898d262020-01-27 16:48:30 -080018 "reflect"
Colin Cross3bbbdf32020-02-05 13:45:11 -080019 "strings"
Jamie Gennisb9cbdae2014-10-02 18:36:13 -070020 "unicode"
21 "unicode/utf8"
22)
23
Colin Cross3bbbdf32020-02-05 13:45:11 -080024// PropertyNameForField converts the name of a field in property struct to the property name that
25// might appear in a Blueprints file. Since the property struct fields must always be exported
26// to be accessed with reflection and the canonical Blueprints style is lowercased names, it
Colin Crossfc6efcb2020-02-05 17:10:18 -080027// lower cases the first rune in the field name unless the field name contains an uppercase rune
28// after the first rune (which is always uppercase), and no lowercase runes.
Jamie Gennisb9cbdae2014-10-02 18:36:13 -070029func PropertyNameForField(fieldName string) string {
30 r, size := utf8.DecodeRuneInString(fieldName)
31 propertyName := string(unicode.ToLower(r))
Colin Cross3bbbdf32020-02-05 13:45:11 -080032 if size == len(fieldName) {
33 return propertyName
34 }
Colin Crossfc6efcb2020-02-05 17:10:18 -080035 if strings.IndexFunc(fieldName[size:], unicode.IsLower) == -1 &&
36 strings.IndexFunc(fieldName[size:], unicode.IsUpper) != -1 {
Colin Cross3bbbdf32020-02-05 13:45:11 -080037 return fieldName
38 }
Jamie Gennisb9cbdae2014-10-02 18:36:13 -070039 if len(fieldName) > size {
40 propertyName += fieldName[size:]
41 }
42 return propertyName
43}
44
Colin Cross3bbbdf32020-02-05 13:45:11 -080045// FieldNameForProperty converts the name of a property that might appear in a Blueprints file to
46// the name of a field in property struct by uppercasing the first rune.
Colin Cross4572edd2015-05-13 14:36:24 -070047func FieldNameForProperty(propertyName string) string {
48 r, size := utf8.DecodeRuneInString(propertyName)
49 fieldName := string(unicode.ToUpper(r))
50 if len(propertyName) > size {
51 fieldName += propertyName[size:]
52 }
53 return fieldName
54}
55
Colin Cross80117682015-10-30 15:53:55 -070056// BoolPtr returns a pointer to a new bool containing the given value.
57func BoolPtr(b bool) *bool {
58 return &b
59}
60
Nan Zhangf5865442017-11-01 14:03:28 -070061// Int64Ptr returns a pointer to a new int64 containing the given value.
62func Int64Ptr(i int64) *int64 {
63 b := int64(i)
64 return &(b)
65}
66
Colin Cross80117682015-10-30 15:53:55 -070067// StringPtr returns a pointer to a new string containing the given value.
68func StringPtr(s string) *string {
69 return &s
70}
71
Colin Crossde3ef3a2018-04-10 14:53:40 -070072// BoolDefault takes a pointer to a bool and returns the value pointed to by the pointer if it is non-nil,
73// or def if the pointer is nil.
74func BoolDefault(b *bool, def bool) bool {
Colin Cross80117682015-10-30 15:53:55 -070075 if b != nil {
76 return *b
77 }
Colin Crossde3ef3a2018-04-10 14:53:40 -070078 return def
79}
80
81// Bool takes a pointer to a bool and returns true iff the pointer is non-nil and points to a true
82// value.
83func Bool(b *bool) bool {
84 return BoolDefault(b, false)
85}
86
87// String takes a pointer to a string and returns the value of the string if the pointer is non-nil,
88// or def if the pointer is nil.
89func StringDefault(s *string, def string) string {
90 if s != nil {
91 return *s
92 }
93 return def
Colin Cross80117682015-10-30 15:53:55 -070094}
95
96// String takes a pointer to a string and returns the value of the string if the pointer is non-nil,
97// or an empty string.
98func String(s *string) string {
Colin Crossde3ef3a2018-04-10 14:53:40 -070099 return StringDefault(s, "")
Colin Cross80117682015-10-30 15:53:55 -0700100}
Colin Cross66c0b132019-09-25 11:25:04 -0700101
102// IntDefault takes a pointer to an int64 and returns the value pointed to by the pointer cast to int
103// if it is non-nil, or def if the pointer is nil.
104func IntDefault(i *int64, def int) int {
105 if i != nil {
106 return int(*i)
107 }
108 return def
109}
110
111// Int takes a pointer to an int64 and returns the value pointed to by the pointer cast to int
112// if it is non-nil, or 0 if the pointer is nil.
113func Int(i *int64) int {
114 return IntDefault(i, 0)
115}
Colin Cross6898d262020-01-27 16:48:30 -0800116
117func isStruct(t reflect.Type) bool {
118 return t.Kind() == reflect.Struct
119}
120
121func isStructPtr(t reflect.Type) bool {
122 return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
123}
Sasha Smundak29fdcad2020-02-11 22:39:47 -0800124
125func isSlice(t reflect.Type) bool {
126 return t.Kind() == reflect.Slice
127}