• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"unicode"
19	"unicode/utf8"
20)
21
22func PropertyNameForField(fieldName string) string {
23	r, size := utf8.DecodeRuneInString(fieldName)
24	propertyName := string(unicode.ToLower(r))
25	if len(fieldName) > size {
26		propertyName += fieldName[size:]
27	}
28	return propertyName
29}
30
31func FieldNameForProperty(propertyName string) string {
32	r, size := utf8.DecodeRuneInString(propertyName)
33	fieldName := string(unicode.ToUpper(r))
34	if len(propertyName) > size {
35		fieldName += propertyName[size:]
36	}
37	return fieldName
38}
39
40// BoolPtr returns a pointer to a new bool containing the given value.
41func BoolPtr(b bool) *bool {
42	return &b
43}
44
45// Int64Ptr returns a pointer to a new int64 containing the given value.
46func Int64Ptr(i int64) *int64 {
47	b := int64(i)
48	return &(b)
49}
50
51// StringPtr returns a pointer to a new string containing the given value.
52func StringPtr(s string) *string {
53	return &s
54}
55
56// BoolDefault takes a pointer to a bool and returns the value pointed to by the pointer if it is non-nil,
57// or def if the pointer is nil.
58func BoolDefault(b *bool, def bool) bool {
59	if b != nil {
60		return *b
61	}
62	return def
63}
64
65// Bool takes a pointer to a bool and returns true iff the pointer is non-nil and points to a true
66// value.
67func Bool(b *bool) bool {
68	return BoolDefault(b, false)
69}
70
71// String takes a pointer to a string and returns the value of the string if the pointer is non-nil,
72// or def if the pointer is nil.
73func StringDefault(s *string, def string) string {
74	if s != nil {
75		return *s
76	}
77	return def
78}
79
80// String takes a pointer to a string and returns the value of the string if the pointer is non-nil,
81// or an empty string.
82func String(s *string) string {
83	return StringDefault(s, "")
84}
85