-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenver.go
73 lines (59 loc) · 1.67 KB
/
enver.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
package enver
import (
"fmt"
"reflect"
"strings"
)
var (
DefaultSources = []Source{
&Source_Env{},
&Source_DotEnv{},
}
)
// Fill fills the given struct with environment variables from the sources.
// The given struct has struct tags to specify the environment variable key and default value.
// The struct tags have the following format:
// `env:"KEY,DEFAULT"`
// The KEY is the environment variable key.
// The DEFAULT is the default value to use if the environment variable is not set.
// The sources are used in the order they are provided. Precedence is given to sources that are provided first.
func Fill(envs any, sources ...Source) error {
if reflect.TypeOf(envs).Kind() != reflect.Ptr {
return fmt.Errorf("envs parameter must be a pointer to a struct")
}
for i := len(sources) - 1; i >= 0; i-- {
envMap, err := sources[i].Get()
if err != nil {
return err
}
err = fill(envs, envMap)
if err != nil {
return err
}
}
return nil
}
func fill(obj interface{}, envMap map[string]any) error {
objType := reflect.TypeOf(obj)
if objType.Kind() != reflect.Ptr {
return fmt.Errorf("setField: obj parameter must be a pointer to a struct")
}
objValue := reflect.ValueOf(obj).Elem()
for i := 0; i < objType.Elem().NumField(); i++ {
field := objType.Elem().Field(i)
envTag := field.Tag.Get("env")
if envTag != "" {
parts := strings.Split(envTag, ",")
envKey := parts[0]
defaultValue := parts[1]
envValue, ok := envMap[envKey]
if !ok {
envValue = defaultValue
}
fieldValue := objValue.FieldByName(field.Name)
convertedValue := reflect.ValueOf(envValue).Convert(fieldValue.Type())
fieldValue.Set(convertedValue)
}
}
return nil
}