-
Notifications
You must be signed in to change notification settings - Fork 3
/
envstruct.go
385 lines (300 loc) · 8.07 KB
/
envstruct.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package envstruct
import (
"fmt"
"net/url"
"os"
"reflect"
"sort"
"strconv"
"strings"
"time"
)
const (
indexEnvVar = 0
tagRequired = "required"
tagReport = "report"
)
// Unmarshaller is a type which unmarshals itself from an environment variable.
type Unmarshaller interface {
UnmarshalEnv(v string) error
}
// Load will use the `env` tags from a struct to populate the structs values and
// perform validations.
func Load(t interface{}) error {
missing, err := load(t)
if err != nil {
return err
}
if len(missing) > 0 {
return fmt.Errorf(
"missing required environment variables: %s",
strings.Join(uniqueStrings(missing), ", "),
)
}
return nil
}
func load(t interface{}) (missing []string, err error) {
val := reflect.ValueOf(t).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
tag := typeField.Tag
tagProperties := separateOnComma(tag.Get("env"))
envVar := tagProperties[indexEnvVar]
envVal := os.Getenv(envVar)
required := tagPropertiesContains(tagProperties, tagRequired)
if isInvalid(envVal, required) {
missing = append(missing, envVar)
continue
}
hasEnvTag := envVar != ""
subMissing, err := setField(valueField, envVal, hasEnvTag)
if err != nil {
return nil, err
}
missing = append(missing, subMissing...)
}
return missing, nil
}
// ToEnv will return a slice of strings that can be used with exec.Cmd.Env
// formatted as `ENVAR_NAME=value` for a given struct.
func ToEnv(t interface{}) []string {
val := reflect.ValueOf(t).Elem()
var results []string
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
tag := typeField.Tag
tagProperties := separateOnComma(tag.Get("env"))
envVar := tagProperties[indexEnvVar]
switch valueField.Kind() {
case reflect.Slice:
results = append(results, formatSlice(envVar, valueField))
case reflect.Map:
results = append(results, formatMap(envVar, valueField))
case reflect.Struct:
results = append(results, ToEnv(valueField.Addr().Interface())...)
case reflect.Ptr:
if valueField.Type() == reflect.TypeOf(&url.URL{}) {
results = append(results, fmt.Sprintf("%s=%+v", envVar, valueField))
continue
}
results = append(results, ToEnv(valueField.Interface())...)
default:
results = append(results, fmt.Sprintf("%s=%+v", envVar, valueField))
}
}
return results
}
func tagPropertiesContains(properties []string, match string) bool {
for _, v := range properties {
if v == match {
return true
}
}
return false
}
func unmarshaller(v reflect.Value) (Unmarshaller, bool) {
if unmarshaller, ok := v.Interface().(Unmarshaller); ok {
return unmarshaller, ok
}
if v.CanAddr() {
return unmarshaller(v.Addr())
}
return nil, false
}
func setField(value reflect.Value, input string, hasEnvTag bool) (missing []string, err error) {
if !value.CanSet() {
return nil, nil
}
if input == "" &&
(value.Kind() != reflect.Ptr) &&
(value.Kind() != reflect.Struct) {
return nil, nil
}
if unmarshaller, ok := unmarshaller(value); ok {
return nil, unmarshaller.UnmarshalEnv(input)
} else {
if value.Kind() == reflect.Struct && hasEnvTag {
return nil, fmt.Errorf("Nested struct %s with env tag needs to have an UnmarshallEnv method\n", value.Type().Name())
}
}
switch value.Type() {
case reflect.TypeOf(time.Second):
return nil, setDuration(value, input)
case reflect.TypeOf(&url.URL{}):
return nil, setURL(value, input)
}
switch value.Kind() {
case reflect.String:
return nil, setString(value, input)
case reflect.Bool:
return nil, setBool(value, input)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return nil, setInt(value, input)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return nil, setUint(value, input)
case reflect.Float32, reflect.Float64:
return nil, setFloat(value, input)
case reflect.Complex64, reflect.Complex128:
return nil, setComplex(value, input)
case reflect.Slice:
return nil, setSlice(value, input, hasEnvTag)
case reflect.Map:
return nil, setMap(value, input)
case reflect.Struct:
return setStruct(value)
case reflect.Ptr:
return setPointerToStruct(value, input, hasEnvTag)
}
return nil, fmt.Errorf("unsupported type %s", value.Kind())
}
func separateOnComma(input string) []string {
inputs := strings.Split(input, ",")
for i, v := range inputs {
inputs[i] = strings.TrimSpace(v)
}
return inputs
}
func isInvalid(input string, required bool) bool {
return required && input == ""
}
func setStruct(value reflect.Value) (missing []string, err error) {
return load(value.Addr().Interface())
}
func setPointerToStruct(value reflect.Value, input string, hasEnvTag bool) (missing []string, err error) {
if value.IsNil() {
p := reflect.New(value.Type().Elem())
value.Set(p)
}
if value.Type().Elem().Kind() == reflect.Struct {
return load(value.Interface())
}
return setField(value.Elem(), input, hasEnvTag)
}
func setDuration(value reflect.Value, input string) error {
d, err := time.ParseDuration(input)
if err != nil {
return err
}
value.Set(reflect.ValueOf(d))
return nil
}
func setURL(value reflect.Value, input string) error {
u, err := url.Parse(input)
if err != nil {
return err
}
value.Set(reflect.ValueOf(u))
return nil
}
func setString(value reflect.Value, input string) error {
value.SetString(input)
return nil
}
func setBool(value reflect.Value, input string) error {
value.SetBool(input == "true" || input == "1")
return nil
}
func setInt(value reflect.Value, input string) error {
n, err := strconv.ParseInt(input, 10, 64)
if err != nil {
return err
}
value.SetInt(int64(n))
return nil
}
func setUint(value reflect.Value, input string) error {
n, err := strconv.ParseUint(input, 10, 64)
if err != nil {
return err
}
value.SetUint(uint64(n))
return nil
}
func setFloat(value reflect.Value, input string) error {
n, err := strconv.ParseFloat(input, 64)
if err != nil {
return err
}
value.SetFloat(float64(n))
return nil
}
func setComplex(value reflect.Value, input string) error {
var n complex128
count, err := fmt.Sscanf(input, "%g", &n)
if err != nil {
return err
}
if count != 1 {
return fmt.Errorf("Expected to parse 1 complex number, found %d", count)
}
value.SetComplex(n)
return nil
}
func setSlice(value reflect.Value, input string, hasEnvTag bool) error {
inputs := separateOnComma(input)
rs := reflect.MakeSlice(value.Type(), len(inputs), len(inputs))
for i, val := range inputs {
_, err := setField(rs.Index(i), val, hasEnvTag)
if err != nil {
return err
}
}
value.Set(rs)
return nil
}
func setMap(value reflect.Value, input string) error {
inputs := separateOnComma(input)
m := reflect.MakeMap(value.Type())
for _, i := range inputs {
kv := strings.SplitN(i, ":", 2)
if len(kv) == 0 {
continue
}
if len(kv) < 2 {
return fmt.Errorf("%s key '%s' is missing a value", value.Type(), kv[0])
}
castedKey := reflect.New(value.Type().Key()).Elem()
castedValue := reflect.New(value.Type().Elem()).Elem()
_, err := setField(castedKey, kv[0], false)
if err != nil {
return fmt.Errorf("setMap: %w", err)
}
_, err = setField(castedValue, kv[1], false)
if err != nil {
return fmt.Errorf("setMap: %w", err)
}
m.SetMapIndex(castedKey, castedValue)
}
value.Set(m)
return nil
}
func formatSlice(envVar string, value reflect.Value) string {
var parts []string
for i := 0; i < value.Len(); i++ {
parts = append(parts, fmt.Sprintf("%+v", value.Index(i)))
}
return fmt.Sprintf("%s=%+v", envVar, strings.Join(parts, ","))
}
func formatMap(envVar string, value reflect.Value) string {
var parts []string
keys := value.MapKeys()
for _, k := range keys {
v := value.MapIndex(k)
parts = append(parts, fmt.Sprintf("%+v:%+v", k, v))
}
return fmt.Sprintf("%s=%+v", envVar, strings.Join(parts, ","))
}
func uniqueStrings(s []string) []string {
m := make(map[string]bool)
for _, str := range s {
m[str] = true
}
res := make([]string, 0, len(m))
for k := range m {
res = append(res, k)
}
sort.Strings(res)
return res
}