-
Notifications
You must be signed in to change notification settings - Fork 0
/
unparse.go
259 lines (209 loc) · 6.62 KB
/
unparse.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
package cnfg
import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
/* This file contains the methods that convert a struct into environment variables. */
type unparser struct {
Low bool // Allow lowercase values in env variable names.
Tag string // struct tag to look for on struct members
}
func (p *unparser) DeconStruct(field reflect.Value, prefix string) (Pairs, error) { //nolint:cyclop
output := Pairs{}
element := field.Type().Elem()
for idx := 0; idx < element.NumField(); idx++ { // Loop each struct member
tagval := strings.Split(element.Field(idx).Tag.Get(p.Tag), ",")
tag := tagval[0]
if !p.Low {
tag = strings.ToUpper(tagval[0]) // like "NAME" or "TIMEOUT"
}
if !field.Elem().Field(idx).CanSet() || tag == "-" {
continue
}
if tag == "" && !element.Field(idx).Anonymous {
tag = element.Field(idx).Name
if !p.Low {
tag = strings.ToUpper(element.Field(idx).Name)
}
}
tag = strings.Trim(strings.Join([]string{prefix, tag}, LevelSeparator), LevelSeparator)
omitempty := false
for i := 1; i < len(tagval); i++ {
if tagval[i] == "omitempty" {
omitempty = true
}
}
o, err := p.Anything(field.Elem().Field(idx), tag, omitempty)
if err != nil {
return nil, err
}
output.Merge(o)
}
return output, nil
}
func (p *unparser) Anything(field reflect.Value, tag string, omitempty bool) (Pairs, error) { //nolint:cyclop
if field.IsZero() && omitempty {
return Pairs{}, nil
}
output, exists, err := p.Interface(field, tag, omitempty)
if err != nil || exists {
return output, err
}
switch field.Kind() {
case reflect.Ptr:
if !field.Elem().CanAddr() {
return output, nil
}
// Pass the non-pointer element back into the start.
return p.Anything(field.Elem().Addr().Elem(), tag, omitempty)
case reflect.Struct:
return p.DeconStruct(field.Addr(), tag)
case reflect.Slice:
return p.Slice(field, tag, omitempty)
case reflect.Map:
return p.Map(field, tag, omitempty)
default:
return p.Member(field, tag, omitempty)
}
}
func (p *unparser) Interface(field reflect.Value, tag string, omitempty bool) (Pairs, bool, error) {
output := Pairs{}
if !field.CanAddr() || !field.Addr().CanInterface() {
pairs, err := p.Member(reflect.ValueOf(field.Interface()), tag, omitempty)
return pairs, true, err
}
if v, ok := field.Addr().Interface().(ENVMarshaler); ok {
// Custom unmarshaler can proceed even if envval is empty. It may produce new envvals...
o, err := v.MarshalENV(tag)
if err != nil {
return output, false, fmt.Errorf("MarshallENV interface: %w", err)
}
output.Merge(o)
return output, true, nil
}
if tag == "" {
return output, false, nil
}
if v, ok := field.Addr().Interface().(encoding.TextMarshaler); ok {
text, err := v.MarshalText()
if err != nil {
return output, false, fmt.Errorf("MarshalText interface: %w", err)
}
output.Set(tag, string(text))
return output, true, nil
}
if v, ok := field.Addr().Interface().(encoding.BinaryMarshaler); ok {
bin, err := v.MarshalBinary()
if err != nil {
return output, false, fmt.Errorf("MarshalText interface: %w", err)
}
output.Set(tag, string(bin))
return output, true, nil
}
return output, false, nil
}
// Member parses non-struct, non-slice struct-member types.
func (p *unparser) Member(field reflect.Value, tag string, omitempty bool) (Pairs, error) { //nolint:cyclop
output := Pairs{}
switch val := field.Interface().(type) {
// Handle each member type appropriately (differently).
case error:
if err, _ := field.Interface().(error); err != nil {
output.Set(tag, err.Error())
}
case string:
output.Set(tag, field.String())
case uint:
output.Set(tag, strconv.FormatUint(uint64(val), base10))
case uint8:
output.Set(tag, strconv.FormatUint(uint64(val), base10))
case uint16:
output.Set(tag, strconv.FormatUint(uint64(val), base10))
case uint32:
output.Set(tag, strconv.FormatUint(uint64(val), base10))
case uint64:
output.Set(tag, strconv.FormatUint(val, base10))
case int:
output.Set(tag, strconv.FormatInt(int64(val), base10))
case int8:
output.Set(tag, strconv.FormatInt(int64(val), base10))
case int16:
output.Set(tag, strconv.FormatInt(int64(val), base10))
case int32:
output.Set(tag, strconv.FormatInt(int64(val), base10))
case int64:
output.Set(tag, strconv.FormatInt(val, base10))
case float64:
output.Set(tag, strconv.FormatFloat(val, 'f', -1, bits64))
case float32:
output.Set(tag, strconv.FormatFloat(float64(val), 'f', -1, bits32))
case time.Duration:
output.Set(tag, (time.Duration(field.Int()) * time.Nanosecond).String())
case bool:
output.Set(tag, strconv.FormatBool(field.Bool()))
default:
return p.kindMember(field, tag, omitempty)
}
return output, nil
}
func (p *unparser) kindMember(field reflect.Value, tag string, _ bool) (Pairs, error) {
output := Pairs{}
switch field.Kind() {
case reflect.String:
output.Set(tag, field.String())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val, _ := field.Interface().(uint64)
output.Set(tag, strconv.FormatUint(val, base10))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val, _ := field.Interface().(int64)
output.Set(tag, strconv.FormatInt(val, base10))
case reflect.Float64:
val, _ := field.Interface().(float64)
output.Set(tag, strconv.FormatFloat(val, 'f', -1, bits64))
case reflect.Float32:
val, _ := field.Interface().(float32)
output.Set(tag, strconv.FormatFloat(float64(val), 'f', -1, bits32))
case reflect.Bool:
output.Set(tag, strconv.FormatBool(field.Bool()))
}
return output, nil
}
func (p *unparser) Slice(field reflect.Value, tag string, omitempty bool) (Pairs, error) {
output := Pairs{}
// slice of bytes works differently than any other slice type.
if field.Type().String() == "[]uint8" {
output.Set(tag, string(field.Bytes()))
return output, nil
}
return p.SliceValue(field, tag, omitempty)
}
func (p *unparser) SliceValue(field reflect.Value, tag string, omitempty bool) (Pairs, error) {
output := Pairs{}
total := field.Len()
for i := 0; i < total; i++ {
ntag := strings.Join([]string{tag, strconv.Itoa(i)}, LevelSeparator)
value := reflect.Indirect(field.Index(i).Addr())
o, err := p.Anything(value, ntag, omitempty)
if err != nil {
return output, err
}
output.Merge(o)
}
return output, nil
}
func (p *unparser) Map(field reflect.Value, tag string, omitempty bool) (Pairs, error) {
output := Pairs{}
for i := field.MapRange(); i.Next(); {
ntag := fmt.Sprintf("%s%s%v", tag, LevelSeparator, i.Key())
o, err := p.Anything(i.Value(), ntag, omitempty)
if err != nil {
return output, err
}
output.Merge(o)
}
return output, nil
}