-
Notifications
You must be signed in to change notification settings - Fork 1
/
envlookup.go
296 lines (273 loc) · 9.11 KB
/
envlookup.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
package conftagz
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
type EnvFieldSubstOpts struct {
// throws an error if the environment variable is not found
ThrowErrorIfEnvMissing bool
}
const ENVFIELD = "env"
func EnvToMap() map[string]string {
envMap := make(map[string]string)
for _, env := range os.Environ() {
splitEnv := strings.SplitN(env, "=", 2)
envMap[splitEnv[0]] = splitEnv[1]
}
return envMap
}
// EnvFieldSubstitution is a function that takes a pointer to a struct
// and looks at each field. If the field has a ENVFIELD tag ("env" by default)
// then it will look up the value of the field in the environment variables
// and replace the field with the value.
// It returns a list of the names of the fields that were substituted - as
// a list of string
// If there is an error, it returns an error
func EnvFieldSubstitution(somestruct interface{}, opts *EnvFieldSubstOpts) (ret []string, err error) {
m := EnvToMap()
return EnvFieldSubstitutionFromMap(somestruct, opts, m)
}
func StringToInt64(s string) (int64, error) {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
fmt.Println(err)
return 0, err
}
return i, nil
}
func StringToUint64(s string) (uint64, error) {
i, err := strconv.ParseUint(s, 10, 64)
if err != nil {
fmt.Println(err)
return 0, err
}
return i, nil
}
func StringToFloat64(s string) (float64, error) {
i, err := strconv.ParseFloat(s, 64)
if err != nil {
fmt.Println(err)
return 0, err
}
return i, nil
}
func addParentPath(parentpath string, fieldname string) string {
if len(parentpath) > 0 {
return parentpath + "." + fieldname
}
return fieldname
}
// EnvFieldSubstitutionFromMap is a function that takes a pointer to a struct
func EnvFieldSubstitutionFromMap(somestruct interface{}, opts *EnvFieldSubstOpts, m map[string]string) (ret []string, err error) {
var throwErrorIfEnvMissing bool
if opts != nil {
throwErrorIfEnvMissing = opts.ThrowErrorIfEnvMissing
}
setEnvVal := func(parentpath string, fieldName string, fieldValue reflect.Value, tag string) error {
if val, ok := m[tag]; ok {
k := fieldValue.Kind()
switch k {
// TODO - add support for Ptr to String and Ints
case reflect.String:
// Change the value of the field to the tag value
fieldValue.SetString(val)
ret = append(ret, addParentPath(parentpath, fieldName))
case reflect.Bool:
// if env var is anything other than empty or "0" or "false"
// then make true
if len(val) > 0 && val != "0" && val != "false" {
fieldValue.SetBool(true)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// Change the value of the field to the tag value
// first convert string to int
nval, err := StringToInt64(val)
if err != nil {
return fmt.Errorf("map (env) %s value %s not a number", tag, val)
}
fieldValue.SetInt(nval)
ret = append(ret, addParentPath(parentpath, fieldName))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// Change the value of the field to the tag value
// first convert string to int
nval, err := StringToUint64(val)
if err != nil {
return fmt.Errorf("map (env) %s value %s not a number", tag, val)
}
fieldValue.SetUint(nval)
ret = append(ret, addParentPath(parentpath, fieldName))
default:
return fmt.Errorf("map (env) for %s underlying type unsupported (setEnvVal)", fieldValue.Type().String())
}
} else {
if throwErrorIfEnvMissing {
return fmt.Errorf("env %s not found", tag)
}
}
return nil
}
setEnvValPtr := func(parentpath string, fieldName string, fieldValue reflect.Value, tag string) error {
if val, ok := m[tag]; ok {
k := fieldValue.Elem().Kind()
switch k {
// TODO - add support for Ptr to String and Ints
case reflect.String:
// Change the value of the field to the tag value
fieldValue.Elem().SetString(val)
ret = append(ret, addParentPath(parentpath, fieldName))
case reflect.Bool:
// if env var is anything other than empty or "0" or "false"
// then make true
if len(val) > 0 && val != "0" && val != "false" {
fieldValue.Elem().SetBool(true)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// Change the value of the field to the tag value
// first convert string to int
nval, err := StringToInt64(val)
if err != nil {
return fmt.Errorf("map (env) %s value %s not a number", tag, val)
}
fieldValue.Elem().SetInt(nval)
ret = append(ret, addParentPath(parentpath, fieldName))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// Change the value of the field to the tag value
// first convert string to int
nval, err := StringToUint64(val)
if err != nil {
return fmt.Errorf("map (env) %s value %s not a number", tag, val)
}
fieldValue.Elem().SetUint(nval)
ret = append(ret, addParentPath(parentpath, fieldName))
default:
return fmt.Errorf("map (env) val for %s underlying type unsupported (setEnvValPtr)", fieldValue.Type().String())
}
return nil
} else {
if throwErrorIfEnvMissing {
return fmt.Errorf("env %s not found", tag)
}
}
return nil
}
var innerSubst func(parentpath string, somestruct interface{}) (err error)
innerSubst = func(parentpath string, somestruct interface{}) (err error) {
// Get the value of the input. This will be a reflect.Value
valuePtr := reflect.ValueOf(somestruct)
if valuePtr.Kind() != reflect.Ptr {
return fmt.Errorf("not a pointer to a struct")
}
inputValue := valuePtr.Elem()
// Get the type of the input. This will be a reflect.Type
inputType := inputValue.Type()
// verify that input is a struct
if inputType.Kind() != reflect.Struct {
return fmt.Errorf("not a struct")
}
// Iterate over all fields of the input
for i := 0; i < inputType.NumField(); i++ {
// Get the field, returns https://golang.org/pkg/reflect/#StructField
field := inputType.Field(i)
// Get the field tag value
tag := field.Tag.Get("env")
// defaultval := field.Tag.Get("default")
conftags := field.Tag.Get("conf")
confops := processConfTagOptsValues(conftags)
// check if confops has a 'skip' key
if skipField(confops) {
continue
}
if envSkip(confops) {
continue
}
debugf("env: Field Name: %s, Env val: %s\n", field.Name, tag)
// if len(defaultval) > 0 {
// Get the field value
fieldValue := inputValue.FieldByName(field.Name)
// Only do substitution if the field value can be changed
if !field.IsExported() {
debugf("env: Field %s is not exported\n", field.Name)
continue
}
if field.Type.Kind() == reflect.Ptr {
// recurse
fieldValue := inputValue.FieldByName(field.Name)
// used to create a temp Value of any kind, based on the underlying
// type of the Ptr - we use this to inspect the underlying type
// if the type is a struct, we always create it
// if the type is a string or number we create it only if we have a default
// value
t := fieldValue.Type()
if fieldValue.IsNil() {
debugf("env: Field %s is nil\n", field.Name)
if skipIfNil(confops) {
continue
}
switch t.Elem().Kind() {
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint16, reflect.Uint8, reflect.Uint32, reflect.Uint64, reflect.Bool:
debugf("env: Ptr: Underlying fundamental type: %s\n", t.Elem().Kind().String())
// Does an env refenced exist?
if _, ok := m[tag]; ok {
fieldValue.Set(reflect.New(t.Elem()))
}
default:
debugf("env: Got a NON-fundamental type: %s %s which is a %s\n", t.Kind().String(), t.Elem().String(), t.Elem().Kind().String())
if fieldValue.CanSet() {
if t.Elem().Kind() == reflect.Struct {
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
} else {
err = fmt.Errorf("default for %s underlying type unsupported", field.Name)
return
}
} else {
debugf("env: Field %s cannot be set (private ?)\n", field.Name)
}
}
}
if !fieldValue.IsNil() {
// is this a Ptr to a struct?
if t.Elem().Kind() == reflect.Struct {
err := innerSubst(addParentPath(parentpath, field.Name), fieldValue.Elem().Addr().Interface())
if err != nil {
return err
}
} else {
// nope then its just a fundamental type
if len(tag) > 0 {
err = setEnvValPtr(parentpath, field.Name, fieldValue, tag)
if err != nil {
return
}
}
}
}
} else if field.Type.Kind() == reflect.Struct {
// recurse
fieldValue := inputValue.FieldByName(field.Name)
// is this a Ptr to a struct?
err := innerSubst(addParentPath(parentpath, field.Name), fieldValue.Addr().Interface())
if err != nil {
return err
}
} else if fieldValue.CanSet() {
if len(tag) > 0 {
err = setEnvVal(parentpath, field.Name, fieldValue, tag)
if err != nil {
return
}
}
} else {
if len(tag) > 0 {
return fmt.Errorf("env for %s cannot be set", field.Name)
}
}
// }
}
return nil
}
err = innerSubst("", somestruct)
return ret, err
}