• 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	"reflect"
19	"strings"
20	"unicode"
21	"unicode/utf8"
22)
23
24func PropertyNameForField(fieldName string) string {
25	r, size := utf8.DecodeRuneInString(fieldName)
26	propertyName := string(unicode.ToLower(r))
27	if len(fieldName) > size {
28		propertyName += fieldName[size:]
29	}
30	return propertyName
31}
32
33func FieldNameForProperty(propertyName string) string {
34	r, size := utf8.DecodeRuneInString(propertyName)
35	fieldName := string(unicode.ToUpper(r))
36	if len(propertyName) > size {
37		fieldName += propertyName[size:]
38	}
39	return fieldName
40}
41
42func HasTag(field reflect.StructField, name, value string) bool {
43	tag := field.Tag.Get(name)
44	for _, entry := range strings.Split(tag, ",") {
45		if entry == value {
46			return true
47		}
48	}
49
50	return false
51}
52
53// BoolPtr returns a pointer to a new bool containing the given value.
54func BoolPtr(b bool) *bool {
55	return &b
56}
57
58// StringPtr returns a pointer to a new string containing the given value.
59func StringPtr(s string) *string {
60	return &s
61}
62
63// Bool takes a pointer to a bool and returns true iff the pointer is non-nil and points to a true
64// value.
65func Bool(b *bool) bool {
66	if b != nil {
67		return *b
68	}
69	return false
70}
71
72// String takes a pointer to a string and returns the value of the string if the pointer is non-nil,
73// or an empty string.
74func String(s *string) string {
75	if s != nil {
76		return *s
77	}
78	return ""
79}
80