-
Notifications
You must be signed in to change notification settings - Fork 1
/
encode.go
196 lines (184 loc) · 4.06 KB
/
encode.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
package lamenv
import (
"encoding"
"fmt"
"os"
"reflect"
"strconv"
"time"
)
func (l *Lamenv) encode(value reflect.Value, parts []string) error {
v := value
// ptr will be used to try if the value is implementing the interface Marshaler.
// if it's the case then, the implementation of the interface has the priority.
ptr := value
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return nil
}
v = v.Elem()
} else {
ptr = reflect.New(v.Type())
ptr.Elem().Set(v)
}
if p, ok := ptr.Interface().(Marshaler); ok {
return p.MarshalEnv(parts)
}
if p, ok := ptr.Interface().(encoding.TextMarshaler); ok {
raw, err := p.MarshalText()
if err != nil {
return err
}
return os.Setenv(buildEnvVariable(parts), string(raw))
}
switch v.Kind() {
case reflect.Map:
if err := l.encodeMap(v, parts); err != nil {
return err
}
case reflect.Slice:
if err := l.encodeSlice(v, parts); err != nil {
return err
}
case reflect.Struct:
if err := l.encodeStruct(v, parts); err != nil {
return err
}
default:
return l.encodeNative(v, buildEnvVariable(parts))
}
return nil
}
func (l *Lamenv) encodeNative(value reflect.Value, input string) error {
return os.Setenv(input, nativeToString(value))
}
func (l *Lamenv) encodeSlice(value reflect.Value, parts []string) error {
if value.IsNil() {
return nil
}
for i := 0; i < value.Len(); i++ {
if err := l.encode(value.Index(i), append(parts, strconv.Itoa(i))); err != nil {
return err
}
}
return nil
}
func (l *Lamenv) encodeMap(value reflect.Value, parts []string) error {
if value.IsNil() {
return nil
}
iter := value.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
if err := l.encode(v, append(parts, nativeToString(k))); err != nil {
return err
}
}
return nil
}
func (l *Lamenv) encodeStruct(value reflect.Value, parts []string) error {
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
fieldType := value.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.encode(field, parts); err != nil {
return err
}
continue
}
if containStr(tags, omitempty) && isZero(field) {
continue
}
} else {
fieldName = fieldType.Name
}
if err := l.encode(value.Field(i), append(parts, fieldName)); err != nil {
return err
}
}
return nil
}
func isZero(v reflect.Value) bool {
kind := v.Kind()
switch kind {
case reflect.String:
return len(v.String()) == 0
case reflect.Interface,
reflect.Ptr:
return v.IsNil()
case reflect.Slice:
return v.Len() == 0
case reflect.Map:
return v.Len() == 0
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
return v.Int() == 0
case reflect.Float32,
reflect.Float64:
return v.Float() == 0
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr:
return v.Uint() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Struct:
vt := v.Type()
for i := 0; i < v.NumField(); i++ {
if len(vt.Field(i).PkgPath) > 0 {
continue // Private field
}
if !isZero(v.Field(i)) {
return false
}
}
return true
}
return false
}
func nativeToString(value reflect.Value) string {
switch value.Kind() {
case reflect.String:
return value.String()
case reflect.Bool:
return fmt.Sprintf("%t", value.Bool())
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
if value.Type() == durationType {
return value.Interface().(time.Duration).String()
}
return fmt.Sprintf("%d", value.Int())
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
return fmt.Sprintf("%d", value.Uint())
case reflect.Float32,
reflect.Float64:
return fmt.Sprintf("%f", value.Float())
}
return ""
}