-
-
Notifications
You must be signed in to change notification settings - Fork 255
/
env.go
848 lines (731 loc) · 20.8 KB
/
env.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// Package env is a simple, zero-dependencies library to parse environment
// variables into structs.
//
// Example:
//
// type config struct {
// Home string `env:"HOME"`
// }
// // parse
// var cfg config
// err := env.Parse(&cfg)
// // or parse with generics
// cfg, err := env.ParseAs[config]()
//
// Check the examples and README for more detailed usage.
package env
import (
"encoding"
"fmt"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"time"
"unicode"
)
// nolint: gochecknoglobals
var (
defaultBuiltInParsers = map[reflect.Kind]ParserFunc{
reflect.Bool: func(v string) (interface{}, error) {
return strconv.ParseBool(v)
},
reflect.String: func(v string) (interface{}, error) {
return v, nil
},
reflect.Int: func(v string) (interface{}, error) {
i, err := strconv.ParseInt(v, 10, 32)
return int(i), err
},
reflect.Int16: func(v string) (interface{}, error) {
i, err := strconv.ParseInt(v, 10, 16)
return int16(i), err
},
reflect.Int32: func(v string) (interface{}, error) {
i, err := strconv.ParseInt(v, 10, 32)
return int32(i), err
},
reflect.Int64: func(v string) (interface{}, error) {
return strconv.ParseInt(v, 10, 64)
},
reflect.Int8: func(v string) (interface{}, error) {
i, err := strconv.ParseInt(v, 10, 8)
return int8(i), err
},
reflect.Uint: func(v string) (interface{}, error) {
i, err := strconv.ParseUint(v, 10, 32)
return uint(i), err
},
reflect.Uint16: func(v string) (interface{}, error) {
i, err := strconv.ParseUint(v, 10, 16)
return uint16(i), err
},
reflect.Uint32: func(v string) (interface{}, error) {
i, err := strconv.ParseUint(v, 10, 32)
return uint32(i), err
},
reflect.Uint64: func(v string) (interface{}, error) {
i, err := strconv.ParseUint(v, 10, 64)
return i, err
},
reflect.Uint8: func(v string) (interface{}, error) {
i, err := strconv.ParseUint(v, 10, 8)
return uint8(i), err
},
reflect.Float64: func(v string) (interface{}, error) {
return strconv.ParseFloat(v, 64)
},
reflect.Float32: func(v string) (interface{}, error) {
f, err := strconv.ParseFloat(v, 32)
return float32(f), err
},
}
)
func defaultTypeParsers() map[reflect.Type]ParserFunc {
return map[reflect.Type]ParserFunc{
reflect.TypeOf(url.URL{}): parseURL,
reflect.TypeOf(time.Nanosecond): parseDuration,
reflect.TypeOf(time.Location{}): parseLocation,
}
}
func parseURL(v string) (interface{}, error) {
u, err := url.Parse(v)
if err != nil {
return nil, newParseValueError("unable to parse URL", err)
}
return *u, nil
}
func parseDuration(v string) (interface{}, error) {
d, err := time.ParseDuration(v)
if err != nil {
return nil, newParseValueError("unable to parse duration", err)
}
return d, err
}
func parseLocation(v string) (interface{}, error) {
loc, err := time.LoadLocation(v)
if err != nil {
return nil, newParseValueError("unable to parse location", err)
}
return *loc, nil
}
// ParserFunc defines the signature of a function that can be used within
// `Options`' `FuncMap`.
type ParserFunc func(v string) (interface{}, error)
// OnSetFn is a hook that can be run when a value is set.
type OnSetFn func(tag string, value interface{}, isDefault bool)
// processFieldFn is a function which takes all information about a field and processes it.
type processFieldFn func(
refField reflect.Value,
refTypeField reflect.StructField,
opts Options,
fieldParams FieldParams,
) error
// Options for the parser.
type Options struct {
// Environment keys and values that will be accessible for the service.
Environment map[string]string
// TagName specifies another tag name to use rather than the default 'env'.
TagName string
// PrefixTagName specifies another prefix tag name to use rather than the default 'envPrefix'.
PrefixTagName string
// DefaultValueTagName specifies another default tag name to use rather than the default 'envDefault'.
DefaultValueTagName string
// RequiredIfNoDef automatically sets all fields as required if they do not
// declare 'envDefault'.
RequiredIfNoDef bool
// OnSet allows to run a function when a value is set.
OnSet OnSetFn
// Prefix define a prefix for every key.
Prefix string
// UseFieldNameByDefault defines whether or not `env` should use the field
// name by default if the `env` key is missing.
// Note that the field name will be "converted" to conform with environment
// variable names conventions.
UseFieldNameByDefault bool
// Custom parse functions for different types.
FuncMap map[reflect.Type]ParserFunc
// Used internally. maps the env variable key to its resolved string value.
// (for env var expansion)
rawEnvVars map[string]string
}
func (opts *Options) getRawEnv(s string) string {
val := opts.rawEnvVars[s]
if val == "" {
return opts.Environment[s]
}
return val
}
func defaultOptions() Options {
return Options{
TagName: "env",
PrefixTagName: "envPrefix",
DefaultValueTagName: "envDefault",
Environment: toMap(os.Environ()),
FuncMap: defaultTypeParsers(),
rawEnvVars: make(map[string]string),
}
}
func customOptions(opt Options) Options {
defOpts := defaultOptions()
if opt.TagName == "" {
opt.TagName = defOpts.TagName
}
if opt.PrefixTagName == "" {
opt.PrefixTagName = defOpts.PrefixTagName
}
if opt.DefaultValueTagName == "" {
opt.DefaultValueTagName = defOpts.DefaultValueTagName
}
if opt.Environment == nil {
opt.Environment = defOpts.Environment
}
if opt.FuncMap == nil {
opt.FuncMap = map[reflect.Type]ParserFunc{}
}
if opt.rawEnvVars == nil {
opt.rawEnvVars = defOpts.rawEnvVars
}
for k, v := range defOpts.FuncMap {
if _, exists := opt.FuncMap[k]; !exists {
opt.FuncMap[k] = v
}
}
return opt
}
func optionsWithSliceEnvPrefix(opts Options, index int) Options {
return Options{
Environment: opts.Environment,
TagName: opts.TagName,
PrefixTagName: opts.PrefixTagName,
DefaultValueTagName: opts.DefaultValueTagName,
RequiredIfNoDef: opts.RequiredIfNoDef,
OnSet: opts.OnSet,
Prefix: fmt.Sprintf("%s%d_", opts.Prefix, index),
UseFieldNameByDefault: opts.UseFieldNameByDefault,
FuncMap: opts.FuncMap,
rawEnvVars: opts.rawEnvVars,
}
}
func optionsWithEnvPrefix(field reflect.StructField, opts Options) Options {
return Options{
Environment: opts.Environment,
TagName: opts.TagName,
PrefixTagName: opts.PrefixTagName,
DefaultValueTagName: opts.DefaultValueTagName,
RequiredIfNoDef: opts.RequiredIfNoDef,
OnSet: opts.OnSet,
Prefix: opts.Prefix + field.Tag.Get(opts.PrefixTagName),
UseFieldNameByDefault: opts.UseFieldNameByDefault,
FuncMap: opts.FuncMap,
rawEnvVars: opts.rawEnvVars,
}
}
// Parse parses a struct containing `env` tags and loads its values from
// environment variables.
func Parse(v interface{}) error {
return parseInternal(v, setField, defaultOptions())
}
// ParseWithOptions parses a struct containing `env` tags and loads its values from
// environment variables.
func ParseWithOptions(v interface{}, opts Options) error {
return parseInternal(v, setField, customOptions(opts))
}
// ParseAs parses the given struct type containing `env` tags and loads its
// values from environment variables.
func ParseAs[T any]() (T, error) {
var t T
err := Parse(&t)
return t, err
}
// ParseWithOptions parses the given struct type containing `env` tags and
// loads its values from environment variables.
func ParseAsWithOptions[T any](opts Options) (T, error) {
var t T
err := ParseWithOptions(&t, opts)
return t, err
}
// Must panic is if err is not nil, and returns t otherwise.
func Must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}
// GetFieldParams parses a struct containing `env` tags and returns information about
// tags it found.
func GetFieldParams(v interface{}) ([]FieldParams, error) {
return GetFieldParamsWithOptions(v, defaultOptions())
}
// GetFieldParamsWithOptions parses a struct containing `env` tags and returns information about
// tags it found.
func GetFieldParamsWithOptions(v interface{}, opts Options) ([]FieldParams, error) {
var result []FieldParams
err := parseInternal(
v,
func(_ reflect.Value, _ reflect.StructField, _ Options, fieldParams FieldParams) error {
if fieldParams.OwnKey != "" {
result = append(result, fieldParams)
}
return nil
},
customOptions(opts),
)
if err != nil {
return nil, err
}
return result, nil
}
func parseInternal(v interface{}, processField processFieldFn, opts Options) error {
ptrRef := reflect.ValueOf(v)
if ptrRef.Kind() != reflect.Ptr {
return newAggregateError(NotStructPtrError{})
}
ref := ptrRef.Elem()
if ref.Kind() != reflect.Struct {
return newAggregateError(NotStructPtrError{})
}
return doParse(ref, processField, opts)
}
func doParse(ref reflect.Value, processField processFieldFn, opts Options) error {
refType := ref.Type()
var agrErr AggregateError
for i := 0; i < refType.NumField(); i++ {
refField := ref.Field(i)
refTypeField := refType.Field(i)
if err := doParseField(refField, refTypeField, processField, opts); err != nil {
if val, ok := err.(AggregateError); ok {
agrErr.Errors = append(agrErr.Errors, val.Errors...)
} else {
agrErr.Errors = append(agrErr.Errors, err)
}
}
}
if len(agrErr.Errors) == 0 {
return nil
}
return agrErr
}
func doParseField(
refField reflect.Value,
refTypeField reflect.StructField,
processField processFieldFn,
opts Options,
) error {
if !refField.CanSet() {
return nil
}
if refField.Kind() == reflect.Ptr && refField.Elem().Kind() == reflect.Struct && !refField.IsNil() {
return parseInternal(refField.Interface(), processField, optionsWithEnvPrefix(refTypeField, opts))
}
if refField.Kind() == reflect.Struct && refField.CanAddr() && refField.Type().Name() == "" {
return parseInternal(refField.Addr().Interface(), processField, optionsWithEnvPrefix(refTypeField, opts))
}
params, err := parseFieldParams(refTypeField, opts)
if err != nil {
return err
}
if params.Ignored {
return nil
}
if err := processField(refField, refTypeField, opts, params); err != nil {
return err
}
if params.Init && isStructPtr(refField) && refField.IsNil() {
refField.Set(reflect.New(refField.Type().Elem()))
refField = refField.Elem()
if _, ok := opts.FuncMap[refField.Type()]; ok {
return nil
}
}
if refField.Kind() == reflect.Struct {
return doParse(refField, processField, optionsWithEnvPrefix(refTypeField, opts))
}
if isSliceOfStructs(refTypeField, opts) {
return doParseSlice(refField, processField, optionsWithEnvPrefix(refTypeField, opts))
}
return nil
}
func isSliceOfStructs(refTypeField reflect.StructField, opts Options) bool {
field := refTypeField.Type
if field.Kind() == reflect.Ptr {
field = field.Elem()
}
if field.Kind() != reflect.Slice {
return false
}
field = field.Elem()
if field.Kind() == reflect.Ptr {
field = field.Elem()
}
_, ignore := defaultBuiltInParsers[field.Kind()]
if !ignore {
_, ignore = opts.FuncMap[field]
}
if !ignore {
_, ignore = reflect.New(field).Interface().(encoding.TextUnmarshaler)
}
if !ignore {
ignore = field.Kind() != reflect.Struct
}
return !ignore
}
func doParseSlice(ref reflect.Value, processField processFieldFn, opts Options) error {
if opts.Prefix != "" && !strings.HasSuffix(opts.Prefix, string(underscore)) {
opts.Prefix += string(underscore)
}
var environments []string
for environment := range opts.Environment {
if strings.HasPrefix(environment, opts.Prefix) {
environments = append(environments, environment)
}
}
if len(environments) > 0 {
counter := 0
for finished := false; !finished; {
finished = true
prefix := fmt.Sprintf("%s%d%c", opts.Prefix, counter, underscore)
for _, variable := range environments {
if strings.HasPrefix(variable, prefix) {
counter++
finished = false
break
}
}
}
sliceType := ref.Type()
var initialized int
if reflect.Ptr == ref.Kind() {
sliceType = sliceType.Elem()
// Due to the rest of code the pre-initialized slice has no chance for this situation
initialized = 0
} else {
initialized = ref.Len()
}
var capacity int
if capacity = initialized; counter > initialized {
capacity = counter
}
result := reflect.MakeSlice(sliceType, capacity, capacity)
for i := 0; i < capacity; i++ {
item := result.Index(i)
if i < initialized {
item.Set(ref.Index(i))
}
if err := doParse(item, processField, optionsWithSliceEnvPrefix(opts, i)); err != nil {
return err
}
}
if result.Len() > 0 {
if reflect.Ptr == ref.Kind() {
resultPtr := reflect.New(sliceType)
resultPtr.Elem().Set(result)
result = resultPtr
}
ref.Set(result)
}
}
return nil
}
func setField(refField reflect.Value, refTypeField reflect.StructField, opts Options, fieldParams FieldParams) error {
value, err := get(fieldParams, opts)
if err != nil {
return err
}
if value != "" {
return set(refField, refTypeField, value, opts.FuncMap)
}
return nil
}
const underscore rune = '_'
func toEnvName(input string) string {
var output []rune
for i, c := range input {
if c == underscore {
continue
}
if len(output) > 0 && unicode.IsUpper(c) {
if len(input) > i+1 {
peek := rune(input[i+1])
if unicode.IsLower(peek) || unicode.IsLower(rune(input[i-1])) {
output = append(output, underscore)
}
}
}
output = append(output, unicode.ToUpper(c))
}
return string(output)
}
// FieldParams contains information about parsed field tags.
type FieldParams struct {
OwnKey string
Key string
DefaultValue string
HasDefaultValue bool
Required bool
LoadFile bool
Unset bool
NotEmpty bool
Expand bool
Init bool
Ignored bool
}
func parseFieldParams(field reflect.StructField, opts Options) (FieldParams, error) {
ownKey, tags := parseKeyForOption(field.Tag.Get(opts.TagName))
if ownKey == "" && opts.UseFieldNameByDefault {
ownKey = toEnvName(field.Name)
}
defaultValue, hasDefaultValue := field.Tag.Lookup(opts.DefaultValueTagName)
result := FieldParams{
OwnKey: ownKey,
Key: opts.Prefix + ownKey,
Required: opts.RequiredIfNoDef,
DefaultValue: defaultValue,
HasDefaultValue: hasDefaultValue,
Ignored: ownKey == "-",
}
for _, tag := range tags {
switch tag {
case "":
continue
case "file":
result.LoadFile = true
case "required":
result.Required = true
case "unset":
result.Unset = true
case "notEmpty":
result.NotEmpty = true
case "expand":
result.Expand = true
case "init":
result.Init = true
case "-":
result.Ignored = true
default:
return FieldParams{}, newNoSupportedTagOptionError(tag)
}
}
return result, nil
}
func get(fieldParams FieldParams, opts Options) (val string, err error) {
var exists, isDefault bool
val, exists, isDefault = getOr(
fieldParams.Key,
fieldParams.DefaultValue,
fieldParams.HasDefaultValue,
opts.Environment,
)
if fieldParams.Expand {
val = os.Expand(val, opts.getRawEnv)
}
opts.rawEnvVars[fieldParams.OwnKey] = val
if fieldParams.Unset {
defer os.Unsetenv(fieldParams.Key)
}
if fieldParams.Required && !exists && fieldParams.OwnKey != "" {
return "", newVarIsNotSetError(fieldParams.Key)
}
if fieldParams.NotEmpty && val == "" {
return "", newEmptyVarError(fieldParams.Key)
}
if fieldParams.LoadFile && val != "" {
filename := val
val, err = getFromFile(filename)
if err != nil {
return "", newLoadFileContentError(filename, fieldParams.Key, err)
}
}
if opts.OnSet != nil {
if fieldParams.OwnKey != "" {
opts.OnSet(fieldParams.Key, val, isDefault)
}
}
return val, err
}
// split the env tag's key into the expected key and desired option, if any.
func parseKeyForOption(key string) (string, []string) {
opts := strings.Split(key, ",")
return opts[0], opts[1:]
}
func getFromFile(filename string) (value string, err error) {
b, err := os.ReadFile(filename)
return string(b), err
}
func getOr(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists, isDefault bool) {
value, exists := envs[key]
switch {
case (!exists || key == "") && defExists:
return defaultValue, true, true
case exists && value == "" && defExists:
return defaultValue, true, true
case !exists:
return "", false, false
}
return value, true, false
}
func set(field reflect.Value, sf reflect.StructField, value string, funcMap map[reflect.Type]ParserFunc) error {
if tm := asTextUnmarshaler(field); tm != nil {
if err := tm.UnmarshalText([]byte(value)); err != nil {
return newParseError(sf, err)
}
return nil
}
typee := sf.Type
fieldee := field
if typee.Kind() == reflect.Ptr {
typee = typee.Elem()
fieldee = field.Elem()
}
parserFunc, ok := funcMap[typee]
if ok {
val, err := parserFunc(value)
if err != nil {
return newParseError(sf, err)
}
fieldee.Set(reflect.ValueOf(val))
return nil
}
parserFunc, ok = defaultBuiltInParsers[typee.Kind()]
if ok {
val, err := parserFunc(value)
if err != nil {
return newParseError(sf, err)
}
fieldee.Set(reflect.ValueOf(val).Convert(typee))
return nil
}
switch field.Kind() {
case reflect.Slice:
return handleSlice(field, value, sf, funcMap)
case reflect.Map:
return handleMap(field, value, sf, funcMap)
}
return newNoParserError(sf)
}
func handleSlice(field reflect.Value, value string, sf reflect.StructField, funcMap map[reflect.Type]ParserFunc) error {
separator := sf.Tag.Get("envSeparator")
if separator == "" {
separator = ","
}
parts := strings.Split(value, separator)
typee := sf.Type.Elem()
if typee.Kind() == reflect.Ptr {
typee = typee.Elem()
}
if _, ok := reflect.New(typee).Interface().(encoding.TextUnmarshaler); ok {
return parseTextUnmarshalers(field, parts, sf)
}
parserFunc, ok := funcMap[typee]
if !ok {
parserFunc, ok = defaultBuiltInParsers[typee.Kind()]
if !ok {
return newNoParserError(sf)
}
}
result := reflect.MakeSlice(sf.Type, 0, len(parts))
for _, part := range parts {
r, err := parserFunc(part)
if err != nil {
return newParseError(sf, err)
}
v := reflect.ValueOf(r).Convert(typee)
if sf.Type.Elem().Kind() == reflect.Ptr {
v = reflect.New(typee)
v.Elem().Set(reflect.ValueOf(r).Convert(typee))
}
result = reflect.Append(result, v)
}
field.Set(result)
return nil
}
func handleMap(field reflect.Value, value string, sf reflect.StructField, funcMap map[reflect.Type]ParserFunc) error {
keyType := sf.Type.Key()
keyParserFunc, ok := funcMap[keyType]
if !ok {
keyParserFunc, ok = defaultBuiltInParsers[keyType.Kind()]
if !ok {
return newNoParserError(sf)
}
}
elemType := sf.Type.Elem()
elemParserFunc, ok := funcMap[elemType]
if !ok {
elemParserFunc, ok = defaultBuiltInParsers[elemType.Kind()]
if !ok {
return newNoParserError(sf)
}
}
separator := sf.Tag.Get("envSeparator")
if separator == "" {
separator = ","
}
keyValSeparator := sf.Tag.Get("envKeyValSeparator")
if keyValSeparator == "" {
keyValSeparator = ":"
}
result := reflect.MakeMap(sf.Type)
for _, part := range strings.Split(value, separator) {
pairs := strings.Split(part, keyValSeparator)
if len(pairs) != 2 {
return newParseError(sf, fmt.Errorf(`%q should be in "key%svalue" format`, part, keyValSeparator))
}
key, err := keyParserFunc(pairs[0])
if err != nil {
return newParseError(sf, err)
}
elem, err := elemParserFunc(pairs[1])
if err != nil {
return newParseError(sf, err)
}
result.SetMapIndex(reflect.ValueOf(key).Convert(keyType), reflect.ValueOf(elem).Convert(elemType))
}
field.Set(result)
return nil
}
func asTextUnmarshaler(field reflect.Value) encoding.TextUnmarshaler {
if field.Kind() == reflect.Ptr {
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
} else if field.CanAddr() {
field = field.Addr()
}
tm, ok := field.Interface().(encoding.TextUnmarshaler)
if !ok {
return nil
}
return tm
}
func parseTextUnmarshalers(field reflect.Value, data []string, sf reflect.StructField) error {
s := len(data)
elemType := field.Type().Elem()
slice := reflect.MakeSlice(reflect.SliceOf(elemType), s, s)
for i, v := range data {
sv := slice.Index(i)
kind := sv.Kind()
if kind == reflect.Ptr {
sv = reflect.New(elemType.Elem())
} else {
sv = sv.Addr()
}
tm := sv.Interface().(encoding.TextUnmarshaler)
if err := tm.UnmarshalText([]byte(v)); err != nil {
return newParseError(sf, err)
}
if kind == reflect.Ptr {
slice.Index(i).Set(sv)
}
}
field.Set(slice)
return nil
}
// ToMap Converts list of env vars as provided by os.Environ() to map you
// can use as Options.Environment field
func ToMap(env []string) map[string]string {
return toMap(env)
}
func isStructPtr(v reflect.Value) bool {
return reflect.Ptr == v.Kind() && v.Type().Elem().Kind() == reflect.Struct
}