-
Notifications
You must be signed in to change notification settings - Fork 2
/
env.go
57 lines (51 loc) · 1.69 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
// Package env is a lightweight library for loading OS environment variables into structs.
package env
import (
"errors"
"fmt"
"github.com/golobby/cast"
"os"
"reflect"
"unsafe"
)
// Feed loads OS environment variables into the given struct.
// It gets the pointer of a struct that is going to holds the variables.
// The struct fields must have an `env` tag that determines the related OS environment variable.
// `env:"APP_NAME"` is a tag example.
func Feed(structure interface{}) error {
inputType := reflect.TypeOf(structure)
if inputType != nil {
if inputType.Kind() == reflect.Ptr {
if inputType.Elem().Kind() == reflect.Struct {
return fillStruct(reflect.ValueOf(structure).Elem())
}
}
}
return errors.New("env: invalid structure")
}
// fillStruct sets a reflected struct fields with the equivalent OS environment variables.
func fillStruct(s reflect.Value) error {
for i := 0; i < s.NumField(); i++ {
if t, exist := s.Type().Field(i).Tag.Lookup("env"); exist {
if osv := os.Getenv(t); osv != "" {
v, err := cast.FromType(osv, s.Type().Field(i).Type)
if err != nil {
return fmt.Errorf("env: cannot set `%v` field; err: %v", s.Type().Field(i).Name, err)
}
ptr := reflect.NewAt(s.Field(i).Type(), unsafe.Pointer(s.Field(i).UnsafeAddr())).Elem()
ptr.Set(reflect.ValueOf(v))
}
} else if s.Type().Field(i).Type.Kind() == reflect.Struct {
if err := fillStruct(s.Field(i)); err != nil {
return err
}
} else if s.Type().Field(i).Type.Kind() == reflect.Ptr {
if s.Field(i).IsZero() == false && s.Field(i).Elem().Type().Kind() == reflect.Struct {
if err := fillStruct(s.Field(i).Elem()); err != nil {
return err
}
}
}
}
return nil
}