-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
274 lines (210 loc) · 6.1 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
package confusing
import (
"encoding/json"
"errors"
"github.com/joho/godotenv"
"os"
"reflect"
"strconv"
"strings"
)
const EnvSourceType SourceType = "env"
type EnvSource struct {
normalizer KeyNormalizer
}
type envQueueItem struct {
key string
target reflect.Value
}
func (s *EnvSource) readEnvKey(key string) string {
key = s.normalizer.Normalize(key)
return os.Getenv(key)
}
// NOTE: Maps and slices of structs/slices don't make sense in environment variables
// Maps are always parsed as JSON strings
// By default, slices are parsed as comma-separated items
// When a slice of structs/slices is encountered, the whole slice is parsed as a JSON string
func (s *EnvSource) readEnvPrimitive(value string, targetValue reflect.Value) error {
targetType := targetValue.Elem().Type()
switch targetType.Kind() {
case reflect.String:
targetValue.Elem().SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
valueInt, err := strconv.Atoi(value)
if err != nil {
return err
}
targetValue.Elem().SetInt(int64(valueInt))
case reflect.Float32, reflect.Float64:
valueFloat, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
targetValue.Elem().SetFloat(valueFloat)
case reflect.Bool:
valueBool, err := parseBool(value)
if err != nil {
return err
}
targetValue.Elem().SetBool(valueBool)
case reflect.Map:
var data map[interface{}]interface{}
var source *MapSource
err := json.Unmarshal([]byte(value), &data)
if err != nil {
return err
}
source, err = NewJSONSource(nil, "")
if err != nil {
return err
}
return source.readMapPrimitive(reflect.ValueOf(data), targetValue)
default:
return errors.New("unsupported target type")
}
return nil
}
func (s *EnvSource) readEnvSlice(value string, targetPtr reflect.Value) error {
sliceType := targetPtr.Elem().Type()
if len(value) > 0 {
switch sliceType.Elem().Kind() {
case reflect.Struct:
fallthrough
case reflect.Slice:
var data []interface{}
var source *MapSource
err := json.Unmarshal([]byte(value), &data)
if err != nil {
return err
}
source, err = NewJSONSource(nil, "")
if err != nil {
return err
}
return source.readMapPrimitive(reflect.ValueOf(data), targetPtr)
default:
valueSlice := strings.Split(value, ",")
valueSliceLen := len(valueSlice)
newSlice := reflect.MakeSlice(sliceType, valueSliceLen, valueSliceLen)
for i := range valueSlice {
valueSlice[i] = strings.TrimSpace(valueSlice[i])
elemType := sliceType.Elem()
elemPtr := reflect.New(elemType)
if err := s.readEnvPrimitive(valueSlice[i], elemPtr); err != nil {
elemPtr.Elem().SetZero()
}
newSlice.Index(i).Set(elemPtr.Elem())
}
targetPtr.Elem().Set(newSlice)
}
} else {
newSlice := reflect.MakeSlice(sliceType, 0, 0)
targetPtr.Elem().Set(newSlice)
}
return nil
}
func (s *EnvSource) readKey(rootKey string, rootTargetValue reflect.Value) error {
queue := []envQueueItem{{rootKey, rootTargetValue}}
for len(queue) > 0 {
item := queue[0]
queue = queue[1:]
targetElemType := item.target.Elem().Type()
targetPtr := item.target
// deference target ptr to the deepest pointer which is pointing at an actual value when the target is a multi-level pointer
for {
if targetElemType.Kind() != reflect.Ptr {
break
} else {
targetElemType = targetElemType.Elem()
elemPtr := reflect.New(targetElemType)
targetPtr.Elem().Set(elemPtr)
targetPtr = elemPtr
}
}
switch targetElemType.Kind() {
case reflect.Slice:
value := strings.TrimSpace(s.readEnvKey(item.key))
if err := s.readEnvSlice(value, targetPtr); err != nil {
return err
}
case reflect.Struct:
reader, isReader := targetPtr.Interface().(Reader)
if isReader {
err := reader.ReadConfig(PrefixSourceWith(item.key, s))
if err != nil {
// errors from custom readers always break execution
// if you want your custom reader to remain fault-tolerant, do not return the errors you get from calling the configSource
return err
}
} else {
for i := 0; i < targetElemType.NumField(); i++ {
field := targetElemType.Field(i)
childKey := processStructField(field)
if childKey == "" {
continue
}
var absoluteKey string
if len(item.key) > 0 {
absoluteKey = concatenateKeys(item.key, childKey)
} else {
absoluteKey = childKey
}
queue = append(queue, envQueueItem{absoluteKey, targetPtr.Elem().Field(i).Addr()})
}
}
default:
if err := s.readEnvPrimitive(s.readEnvKey(item.key), targetPtr); err != nil {
// targetPtr.Elem().SetZero()
continue
}
}
}
return nil
}
func (s *EnvSource) ReadKey(key string, target interface{}) error {
targetValue := reflect.ValueOf(target)
if targetValue.Kind() != reflect.Ptr || targetValue.IsNil() {
return errors.New("target must be a non-nil pointer")
}
return s.readKey(key, targetValue)
}
func (s *EnvSource) Read(target interface{}) error {
targetValue := reflect.ValueOf(target)
if targetValue.Kind() != reflect.Ptr || targetValue.IsNil() {
return errors.New("target must be a non-nil pointer")
}
targetType := targetValue.Elem().Type()
if targetType.Kind() != reflect.Struct {
return errors.New("target must be a struct")
}
return s.readKey("", targetValue)
}
func (s *EnvSource) Type() string {
return EnvSourceType
}
func NewEnvSource(convention string) (*EnvSource, error) {
normalizer, err := NormalizerForSourceType(convention, EnvSourceType)
if err != nil {
return nil, err
}
return &EnvSource{normalizer: normalizer}, nil
}
// BuildEnvSource This function only fails if the .env file path is explicitly provided and doesn't exist
func BuildEnvSource(opts SourceOptions) (Source, error) {
var err error
var verifyPath bool
if len(opts.FilePath) > 0 {
err = godotenv.Load(opts.FilePath)
verifyPath = true
} else {
err = godotenv.Load()
verifyPath = false
}
isFileExistsErr := errors.Is(err, os.ErrNotExist)
if err != nil {
if (isFileExistsErr && verifyPath) || !isFileExistsErr {
return nil, err
}
}
return NewEnvSource(opts.Convention)
}