-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode.go
294 lines (273 loc) · 8.48 KB
/
decode.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
package lamenv
import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
const (
omitempty = "omitempty"
squash = "squash"
inline = "inline"
)
var durationType = reflect.TypeOf(time.Duration(0))
func (l *Lamenv) decode(conf reflect.Value, parts []string) error {
v := conf
// ptr will be used to try if the value is implementing the interface Unmarshaler.
// if it's the case then, the implementation of the interface has the priority.
ptr := conf
if v.Kind() == reflect.Ptr {
if v.IsNil() {
// if the pointer is not initialized, then accessing to its element will return `reflect.invalid`
// So we have to create a new instance of the pointer first
v.Set(reflect.New(v.Type().Elem()))
ptr = v
}
v = v.Elem()
} else {
ptr = reflect.New(v.Type())
ptr.Elem().Set(v)
}
if p, ok := ptr.Interface().(Unmarshaler); ok {
if err := p.UnmarshalEnv(parts); err != nil {
return err
}
// in case the method UnmarshalEnv() is setting some parameter in the struct, we have to save these changes
v.Set(ptr.Elem())
return nil
}
if p, ok := ptr.Interface().(encoding.TextUnmarshaler); ok {
if variable, input, exist := lookupEnv(parts); exist {
// remove the variable to avoid reusing it later
delete(l.env, variable)
if err := p.UnmarshalText([]byte(input)); err != nil {
return err
}
// in case the method UnmarshalEnv() is setting some parameter in the struct, we have to save these changes
v.Set(ptr.Elem())
}
return nil
}
switch v.Kind() {
case reflect.Map:
if err := l.decodeMap(v, parts); err != nil {
return err
}
case reflect.Slice:
if err := l.decodeSlice(v, parts); err != nil {
return err
}
case reflect.Struct:
if err := l.decodeStruct(v, parts); err != nil {
return err
}
default:
if variable, input, exist := lookupEnv(parts); exist {
// remove the variable to avoid to reuse it later
delete(l.env, variable)
return l.decodeNative(v, input)
}
}
return nil
}
func (l *Lamenv) decodeNative(v reflect.Value, input string) error {
switch v.Kind() {
case reflect.String:
l.decodeString(v, input)
case reflect.Bool:
if err := l.decodeBool(v, input); err != nil {
return err
}
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
if err := l.decodeInt(v, input); err != nil {
return err
}
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
if err := l.decodeUInt(v, input); err != nil {
return err
}
case reflect.Float32,
reflect.Float64:
if err := l.decodeFloat(v, input); err != nil {
return err
}
}
return nil
}
func (l *Lamenv) decodeString(v reflect.Value, input string) {
v.SetString(input)
}
func (l *Lamenv) decodeBool(v reflect.Value, input string) error {
b, err := strconv.ParseBool(strings.TrimSpace(input))
if err != nil {
return err
}
v.SetBool(b)
return nil
}
func (l *Lamenv) decodeInt(v reflect.Value, input string) error {
if v.Type() == durationType {
i, err := time.ParseDuration(strings.TrimSpace(input))
if err != nil {
return err
}
v.SetInt(int64(i))
} else {
i, err := strconv.ParseInt(strings.TrimSpace(input), 10, 0)
if err != nil {
return err
}
v.SetInt(i)
}
return nil
}
func (l *Lamenv) decodeUInt(v reflect.Value, input string) error {
i, err := strconv.ParseUint(strings.TrimSpace(input), 10, 0)
if err != nil {
return err
}
v.SetUint(i)
return nil
}
func (l *Lamenv) decodeFloat(v reflect.Value, input string) error {
i, err := strconv.ParseFloat(strings.TrimSpace(input), 64)
if err != nil {
return err
}
v.SetFloat(i)
return nil
}
// decodeSlice will support ony one syntax which is:
//
// <PREFIX>_<SLICE_INDEX>(_<SUFFIX>)?
//
// This syntax is the only one that is able to manage smoothly every existing type in Golang and it is a determinist syntax.
func (l *Lamenv) decodeSlice(v reflect.Value, parts []string) error {
sliceType := v.Type().Elem()
// While we are able to find an environment variable that is starting by <PREFIX>_<SLICE_INDEX>
// then it will create a new item in a slice and will use the next recursive loop to set it.
i := 0
for ok := contains(append(parts, strconv.Itoa(i))); ok; ok = contains(append(parts, strconv.Itoa(i))) {
var sliceElem reflect.Value
if i < v.Len() {
// that means there is already an element in the slice and should just complete or override the value
sliceElem = v.Index(i)
} else {
// in that case we have to create a new element
sliceElem = reflect.Indirect(reflect.New(sliceType))
}
if err := l.decode(sliceElem, append(parts, strconv.Itoa(i))); err != nil {
return err
}
if i >= v.Len() {
// in case we have created a new element, then we need to add it to the slice
v.Set(reflect.Append(v, sliceElem))
}
i++
}
return nil
}
func (l *Lamenv) decodeStruct(v reflect.Value, parts []string) error {
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldType := v.Type().Field(i)
if len(fieldType.PkgPath) > 0 {
// the field is not exported, so no need to look at it as we won't be able to set it in a later stage
continue
}
var fieldName string
tags, ok := l.lookupTag(fieldType.Tag)
if ok {
fieldName = tags[0]
tags = tags[1:]
if fieldName == "-" {
continue
}
if containStr(tags, squash) || containStr(tags, inline) {
if err := l.decode(field, parts); err != nil {
return err
}
continue
}
if containStr(tags, omitempty) {
// Here we only have to check if there is one environment variable that is starting by the current parts
// It's not necessary accurate if you have one field that is a prefix of another field.
// But it's not really a big deal since it will just loop another time for nothing and could eventually initialize the field. But this case will not occur so often.
// To be more accurate, we would have to check the type of the field, because if it's a native type, then we will have to check if the parts are matching an environment variable.
// If it's a struct or an array or a map, then we will have to check if there is at least one variable starting by the parts + "_" (which would remove the possibility of having a field being a prefix of another one)
// So it's simpler like that. Let's see if I'm wrong or not.
if !contains(append(parts, fieldName)) {
continue
}
}
} else {
fieldName = fieldType.Name
}
if err := l.decode(field, append(parts, fieldName)); err != nil {
return err
}
}
return nil
}
func (l *Lamenv) decodeMap(v reflect.Value, parts []string) error {
keyType := v.Type().Key()
valueType := v.Type().Elem()
if keyType.Kind() != reflect.String {
return fmt.Errorf("unable to unmarshal a map with a key that is not a string")
}
if valueType.Kind() == reflect.Map {
return fmt.Errorf("unable to unmarshal a map of a map, it's not a determinist datamodel")
}
valMap := v
if v.IsNil() {
mapType := reflect.MapOf(keyType, valueType)
valMap = reflect.MakeMap(mapType)
}
// The main issue with the map when you are dealing with environment variable is to be able to find the key of the map
// A way to achieve it is to take a look at the type of the value of the map.
// It will be used to find every potential future parts, which will be then used as a variable suffix.
// Like that we are able catch the key that would be in the middle of the prefix parts and the future parts
// Let's create first the struct that would represent what is behind the value of the map
parser := newRing(valueType, l.tagSupports)
// then foreach environment variable:
// 1. Remove the prefix parts
// 2. Pass the remaining parts to the parser that would return the prefix to be used.
for e := range l.env {
variable := buildEnvVariable(parts)
trimEnv := strings.TrimPrefix(e, variable+"_")
if trimEnv == e {
// TrimPrefix didn't remove anything, so that means, the environment variable doesn't start with the prefix parts
continue
}
futureParts := strings.Split(trimEnv, "_")
prefix, err := guessPrefix(futureParts, parser)
if err != nil {
return err
}
if len(prefix) == 0 {
// no prefix find, let's move to the next environment
continue
}
keyString := strings.ToLower(prefix)
value := reflect.Indirect(reflect.New(valueType))
if err := l.decode(value, append(parts, keyString)); err != nil {
return err
}
key := reflect.Indirect(reflect.New(reflect.TypeOf("")))
key.SetString(strings.TrimSpace(strings.ToLower(keyString)))
valMap.SetMapIndex(key, value)
}
// Set the built up map to the value
v.Set(valMap)
return nil
}