1// 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
15package proptools
16
17import (
18	"reflect"
19	"strings"
20	"unicode"
21	"unicode/utf8"
22)
23
24// 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
27// 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.
29func PropertyNameForField(fieldName string) string {
30	r, size := utf8.DecodeRuneInString(fieldName)
31	propertyName := string(unicode.ToLower(r))
32	if size == len(fieldName) {
33		return propertyName
34	}
35	if strings.IndexFunc(fieldName[size:], unicode.IsLower) == -1 &&
36		strings.IndexFunc(fieldName[size:], unicode.IsUpper) != -1 {
37		return fieldName
38	}
39	if len(fieldName) > size {
40		propertyName += fieldName[size:]
41	}
42	return propertyName
43}
44
45// 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.
47func 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
56// BoolPtr returns a pointer to a new bool containing the given value.
57func BoolPtr(b bool) *bool {
58	return &b
59}
60
61// 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
67// StringPtr returns a pointer to a new string containing the given value.
68func StringPtr(s string) *string {
69	return &s
70}
71
72// 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 {
75	if b != nil {
76		return *b
77	}
78	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
94}
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 {
99	return StringDefault(s, "")
100}
101
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}
116
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}
124
125func isSlice(t reflect.Type) bool {
126	return t.Kind() == reflect.Slice
127}
128