-
Notifications
You must be signed in to change notification settings - Fork 2
/
env_tree.go
362 lines (292 loc) · 10.8 KB
/
env_tree.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package envh
import (
"regexp"
"strings"
)
// EnvTree manages environment variables through a tree structure
// to store a config the same way as in a yaml file or whatever
// format allows to store a config hierarchically
type EnvTree struct {
root *node
}
// NewEnvTree creates an environment variable tree.
// A delimiter is used to split key, reg is a regexp
// used to filter entries
func NewEnvTree(reg string, delimiter string) (EnvTree, error) {
r, err := regexp.Compile(reg)
if err != nil {
return EnvTree{}, err
}
t := createTreeFromDelimiterFilteringByRegexp(r, delimiter)
return EnvTree{t}, nil
}
// FindString returns a string if key chain exists
// or an error otherwise
func (e EnvTree) FindString(keyChain ...string) (string, error) {
return getString(getNodeValueByKeyChain(e.root, &keyChain))
}
// FindStringUnsecured is insecured version of FindString to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing, it returns default zero string value.
// This function has to be used carefully
func (e EnvTree) FindStringUnsecured(keyChain ...string) string {
if val, err := getString(getNodeValueByKeyChain(e.root, &keyChain)); err == nil {
return val
}
return ""
}
// FindInt returns an integer if key chain exists
// or an error if value is not an integer or doesn't exist
func (e EnvTree) FindInt(keyChain ...string) (int, error) {
return getInt(getNodeValueByKeyChain(e.root, &keyChain))
}
// FindIntUnsecured is insecured version of FindInt to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not an int value, it returns default zero int value.
// This function has to be used carefully
func (e EnvTree) FindIntUnsecured(keyChain ...string) int {
if val, err := getInt(getNodeValueByKeyChain(e.root, &keyChain)); err == nil {
return val
}
return 0
}
// FindFloat returns a float if key chain exists
// or an error if value is not a float or doesn't exist
func (e EnvTree) FindFloat(keyChain ...string) (float32, error) {
return getFloat(getNodeValueByKeyChain(e.root, &keyChain))
}
// FindFloatUnsecured is insecured version of FindFloat to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not a floating value, it returns default zero floating value.
// This function has to be used carefully
func (e EnvTree) FindFloatUnsecured(keyChain ...string) float32 {
if val, err := getFloat(getNodeValueByKeyChain(e.root, &keyChain)); err == nil {
return val
}
return 0
}
// FindBool returns a boolean if key chain exists
// or an error if value is not a boolean or doesn't exist
func (e EnvTree) FindBool(keyChain ...string) (bool, error) {
return getBool(getNodeValueByKeyChain(e.root, &keyChain))
}
// FindBoolUnsecured is insecured version of FindBool to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not a boolean value, it returns default zero boolean value.
// This function has to be used carefully
func (e EnvTree) FindBoolUnsecured(keyChain ...string) bool {
if val, err := getBool(getNodeValueByKeyChain(e.root, &keyChain)); err == nil {
return val
}
return false
}
// IsExistingSubTree returns true if key chain has a tree associated or false if not
func (e EnvTree) IsExistingSubTree(keyChain ...string) bool {
_, exists := e.root.findNodeByKeyChain(&keyChain)
return exists
}
// HasSubTreeValue returns true if key chain has a value or false if not.
// If sub node doesn't exist, it returns an error ErrNodeNotFound
// as second value
func (e EnvTree) HasSubTreeValue(keyChain ...string) (bool, error) {
n, exists := e.root.findNodeByKeyChain(&keyChain)
if !exists {
return false, NodeNotFoundError{keyChain}
}
return n.hasValue, nil
}
// HasSubTreeValueUnsecured is insecured version of HasSubTreeValue to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the node doesn't exist, it returns false.
// This function has to be used carefully
func (e EnvTree) HasSubTreeValueUnsecured(keyChain ...string) bool {
n, exists := e.root.findNodeByKeyChain(&keyChain)
if !exists {
return false
}
return n.hasValue
}
// FindSubTree returns underlying tree from key chain,
// for instance given A -> B -> C -> D tree,
// "A" "B" "C" key chain will return C sub tree.
// If no node is found, it returns an error ErrNodeNotFound as
// second value
func (e EnvTree) FindSubTree(keyChain ...string) (EnvTree, error) {
if n, exists := e.root.findNodeByKeyChain(&keyChain); exists {
return EnvTree{n}, nil
}
return EnvTree{}, NodeNotFoundError{keyChain}
}
// FindSubTreeUnsecured is insecured version of FindSubTree to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the node doesn't exist, it returns empty EnvTree.
// This function has to be used carefully
func (e EnvTree) FindSubTreeUnsecured(keyChain ...string) EnvTree {
if n, exists := e.root.findNodeByKeyChain(&keyChain); exists {
return EnvTree{n}
}
return EnvTree{}
}
// FindChildrenKeys returns all children keys for a given key chain.
// If sub node doesn't exist, it returns an error ErrNodeNotFound
// as second value
func (e EnvTree) FindChildrenKeys(keyChain ...string) ([]string, error) {
n, exists := e.root.findNodeByKeyChain(&keyChain)
if !exists {
return []string{}, NodeNotFoundError{keyChain}
}
keys := []string{}
for _, c := range n.children {
keys = append(keys, c.key)
}
return keys, nil
}
// FindChildrenKeysUnsecured is insecured version of FindChildrenKeys to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the node doesn't exist, it returns empty string slice.
// This function has to be used carefully
func (e EnvTree) FindChildrenKeysUnsecured(keyChain ...string) []string {
n, exists := e.root.findNodeByKeyChain(&keyChain)
if !exists {
return []string{}
}
keys := []string{}
for _, c := range n.children {
keys = append(keys, c.key)
}
return keys
}
// GetChildrenKeys retrieves all current tree children node keys
func (e EnvTree) GetChildrenKeys() []string {
keys := []string{}
for _, c := range e.root.children {
keys = append(keys, c.key)
}
return keys
}
// GetString returns current tree value as string if value exists
// or an error as second parameter
func (e EnvTree) GetString() (string, error) {
return getString(getRootValue(e))
}
// GetStringUnsecured is insecured version of GetString to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing, it returns default zero string value.
// This function has to be used carefully
func (e EnvTree) GetStringUnsecured() string {
if val, err := getString(getRootValue(e)); err == nil {
return val
}
return ""
}
// GetInt returns current tree value as int if value exists
// or an error if value is not an integer or doesn't exist
func (e EnvTree) GetInt() (int, error) {
return getInt(getRootValue(e))
}
// GetIntUnsecured is insecured version of GetInt to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not an int value, it returns default zero int value.
// This function has to be used carefully
func (e EnvTree) GetIntUnsecured() int {
if val, err := getInt(getRootValue(e)); err == nil {
return val
}
return 0
}
// GetFloat returns current tree value as float if value exists
// or an error if value is not a float or doesn't exist
func (e EnvTree) GetFloat() (float32, error) {
return getFloat(getRootValue(e))
}
// GetFloatUnsecured is insecured version of GetFloat to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not a floating value, it returns default zero floating value.
// This function has to be used carefully
func (e EnvTree) GetFloatUnsecured() float32 {
if val, err := getFloat(getRootValue(e)); err == nil {
return val
}
return 0
}
// GetBool returns current tree value as boolean if value exists
// or an error if value is not a boolean or doesn't exist
func (e EnvTree) GetBool() (bool, error) {
return getBool(getRootValue(e))
}
// GetBoolUnsecured is insecured version of GetBool to avoid the burden
// of rechecking errors if it was done already. If any errors occurred cause
// the variable is missing or not a boolean value, it returns default zero boolean value.
// This function has to be used carefully
func (e EnvTree) GetBoolUnsecured() bool {
if val, err := getBool(getRootValue(e)); err == nil {
return val
}
return false
}
// HasValue returns true if current tree has a value defined
// false otherwise
func (e EnvTree) HasValue() bool {
return e.root.hasValue
}
// GetKey returns current tree key
func (e EnvTree) GetKey() string {
return e.root.key
}
// PopulateStruct fills a structure with datas extracted.
// Missing values are ignored and only type errors are reported.
// It's possible to control the way struct fields are defined
// implementing StructWalker interface on structure,
// checkout StructWalker documentation for further examples.
func (e EnvTree) PopulateStruct(structure interface{}) error {
return populateStructFromEnvTree(structure, &e, false)
}
// PopulateStructWithStrictMode fills a structure with datas extracted.
// A missing environment variable returns an error and type errors are reported.
// It's possible to control the way struct fields are defined
// implementing StructWalker interface on structure,
// checkout StructWalker documentation for further examples.
func (e EnvTree) PopulateStructWithStrictMode(structure interface{}) error {
return populateStructFromEnvTree(structure, &e, true)
}
func getRootValue(tree EnvTree) func() (string, bool) {
return func() (string, bool) {
if tree.root.hasValue {
return tree.root.value, true
}
return "", false
}
}
func createBranch(key string, value string, delimiter string, current *node) {
for _, component := range strings.Split(key, delimiter) {
n, exists := current.findNodeByKey(component)
if exists {
current = n
} else {
child := newNode()
child.key = component
current.appendNode(child)
current = child
}
}
current.hasValue = true
current.value = value
}
func createTreeFromDelimiterFilteringByRegexp(reg *regexp.Regexp, delimiter string) *node {
rootNode := newNode()
for key, value := range *parseVars() {
if reg.MatchString(key) {
createBranch(key, value, delimiter, rootNode)
}
}
return rootNode
}
func getNodeValueByKeyChain(node *node, keyChain *[]string) func() (string, bool) {
return func() (string, bool) {
n, exists := node.findNodeByKeyChain(keyChain)
if !exists {
return "", false
}
return n.value, true
}
}