-
Notifications
You must be signed in to change notification settings - Fork 2
/
envstruct.go
189 lines (147 loc) · 3.55 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
package envstruct
import (
"fmt"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"time"
)
const (
indexEnvVar = 0
tagRequired = "required"
tagNoReport = "noreport"
)
// 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 {
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 := extractSliceInputs(tag.Get("env"))
envVar := strings.ToUpper(tagProperties[indexEnvVar])
envVal := os.Getenv(envVar)
required := tagPropertiesContains(tagProperties, tagRequired)
if isInvalid(envVal, required) {
return fmt.Errorf("%s is required but was empty", envVar)
}
if envVal == "" {
continue
}
err := setField(valueField, envVal)
if err != nil {
return err
}
}
return nil
}
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) error {
if unmarshaller, ok := unmarshaller(value); ok {
return unmarshaller.UnmarshalEnv(input)
}
switch value.Type() {
case reflect.TypeOf(time.Second):
return setDuration(value, input)
case reflect.TypeOf(&url.URL{}):
return setURL(value, input)
}
switch value.Kind() {
case reflect.String:
return setString(value, input)
case reflect.Bool:
return setBool(value, input)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return setInt(value, input)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return setUint(value, input)
case reflect.Slice:
return setSlice(value, input)
}
return nil
}
func extractSliceInputs(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 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 setSlice(value reflect.Value, input string) error {
inputs := extractSliceInputs(input)
rs := reflect.MakeSlice(value.Type(), len(inputs), len(inputs))
for i, val := range inputs {
err := setField(rs.Index(i), val)
if err != nil {
return err
}
}
value.Set(rs)
return nil
}