-
Notifications
You must be signed in to change notification settings - Fork 2
/
field.go
114 lines (92 loc) · 2.66 KB
/
field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package config
import (
"reflect"
"strings"
)
// Tag is the key of the field Tag that is used to match certain things and properties,
// currently only "required", i.e myField `config:"required"`.
//
// Can be changed to a custom one if needed.
var Tag = "config"
func lookupTag(f reflect.StructField) (string, bool) {
tag := f.Tag.Get(Tag)
if tag == "" {
return "", false
}
return tag, true
}
func containsTagValue(f reflect.StructField, searchFor string) bool {
tag, found := lookupTag(f)
if !found {
return false
}
if tag == searchFor {
return true
}
values := strings.Split(tag, ",")
for _, v := range values {
if v == searchFor {
return true
}
}
return false
}
type field struct {
// the actual struct's indexes of the field.
Index []int
// the actual name, the yaml(or other file decoder's tag name) one or the field name.
Name string
// if marked as required, by tag.
// And as always; a bool false value is zero,
// so if required then it will ask for it so make sure that you setup your configuration fields correctly,
// look the "options" structure's guidelines on comments to see what I'm talking about.
Required bool
// true if it's password/secret, tag value contains "password" or "secret", it's being used
// on survey to show a special password prompt.
Secret bool
}
func structFieldIgnored(f reflect.StructField) bool {
return containsTagValue(f, "-") // if the tag contains the "-" then ignore it.
}
func isRequired(f reflect.StructField) bool {
if f.Anonymous || f.PkgPath != "" || f.Type.Kind() == reflect.Struct {
// skip unexported, anonymous(embedded) and structs from this check, they are always false,
// because the check should be happen on the fields of these structs and not on these as structures.
return false
}
return !structFieldIgnored(f)
}
func isSecret(f reflect.StructField) bool {
return containsTagValue(f, "password") || containsTagValue(f, "secret")
}
func lookupFields(typ reflect.Type, parent field) (fields []field) {
for i, n := 0, typ.NumField(); i < n; i++ {
f := typ.Field(i)
if f.Type.Kind() == reflect.Ptr {
continue // skip pointers.
}
index := []int{i}
// embedded.
if f.Type.Kind() == reflect.Struct && !structFieldIgnored(f) {
fields = append(fields, lookupFields(f.Type, field{
Name: f.Name,
Index: index,
})...)
continue
}
required := isRequired(f)
name := f.Name
if len(parent.Index) > 0 && parent.Index[0] >= 0 {
index = append(parent.Index, index...)
name = parent.Name + "." + name
}
field := field{
Name: name,
Index: index,
Required: required,
Secret: isSecret(f),
}
fields = append(fields, field)
}
return
}