forked from vrischmann/envconfig
/
envconfig.go
306 lines (258 loc) · 7.27 KB
/
envconfig.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
295
296
297
298
299
300
301
302
303
304
305
306
package envconfig
import (
"encoding/base64"
"errors"
"reflect"
"strconv"
"strings"
"time"
)
var (
// ErrUnexportedField is the error returned by the Init* functions when a field of the config struct is not exported and the option AllowUnexported is not used.
ErrUnexportedField = errors.New("envconfig: unexported field")
// ErrNotAPointer is the error returned by the Init* functions when the configuration object is not a pointer.
ErrNotAPointer = errors.New("envconfig: value is not a pointer")
// ErrInvalidValueKind is the error returned by the Init* functions when the configuration object is not a struct.
ErrInvalidValueKind = errors.New("envconfig: invalid value kind, only works on structs")
)
// ConfInfo stores information about a configuration struct.
type ConfInfo []*Field
func (cinfo *ConfInfo) append(fld *Field) {
*cinfo = append(*cinfo, fld)
}
// Read reads the configuration from environment variables and populates the conf object.
func (cinfo *ConfInfo) Read() error {
for _, fld := range *cinfo {
if err := fld.setValue(); err != nil {
return err
}
}
return nil
}
type context struct {
config *ConfInfo
name fieldName
optional bool
allowUnexported bool
}
// Unmarshaler is the interface implemented by objects that can unmarshal a environment variable string of themselves.
type Unmarshaler interface {
Unmarshal(s string) error
}
// Options is used to customize the behavior of envconfig. Use it with InitWithOptions.
type Options struct {
// Prefix allows specifying a prefix for each key.
Prefix string
// AllOptional determines whether to not throw errors by default for any key
// that is not found. AllOptional=true means errors will not be thrown.
AllOptional bool
// AllowUnexported allows unexported fields to be present in the passed config.
AllowUnexported bool
}
// Init reads the configuration from environment variables and populates the conf object.
// conf must be a pointer
func Init(conf interface{}) error {
return InitWithOptions(conf, Options{})
}
// InitWithPrefix reads the configuration from environment variables and populates the conf object.
// conf must be a pointer.
// Each key read will be prefixed with the prefix string.
func InitWithPrefix(conf interface{}, prefix string) error {
return InitWithOptions(conf, Options{Prefix: prefix})
}
// InitWithOptions reads the configuration from environment variables and populates the conf object.
// conf must be a pointer.
func InitWithOptions(conf interface{}, opts Options) error {
cinfo, err := ParseWithOptions(conf, opts)
if err != nil {
return err
}
return cinfo.Read()
}
// Parse returns a ConfInfo object and sets up the conf object to be populated with Read().
// conf must be a pointer
func Parse(conf interface{}) (*ConfInfo, error) {
return ParseWithOptions(conf, Options{})
}
// ParseWithPrefix returns a ConfInfo object and sets up the conf object to be populated with Read().
// conf must be a pointer.
// Each key will be prefixed with the prefix string.
func ParseWithPrefix(conf interface{}, prefix string) (*ConfInfo, error) {
return ParseWithOptions(conf, Options{Prefix: prefix})
}
// ParseWithOptions returns a ConfInfo object and sets up the conf object to be populated with Read().
// conf must be a pointer.
func ParseWithOptions(conf interface{}, opts Options) (*ConfInfo, error) {
value := reflect.ValueOf(conf)
if value.Kind() != reflect.Ptr {
return nil, ErrNotAPointer
}
elem := value.Elem()
for elem.Kind() == reflect.Ptr {
if elem.IsNil() {
elem.Set(reflect.New(elem.Type().Elem()))
}
elem = elem.Elem()
}
if elem.Kind() != reflect.Struct {
return nil, ErrInvalidValueKind
}
name := fieldName{}
if opts.Prefix != "" {
name = name.Append(opts.Prefix)
}
cinfo := &ConfInfo{}
return cinfo, readStruct(elem, &context{
config: cinfo,
name: name,
optional: opts.AllOptional,
allowUnexported: opts.AllowUnexported,
})
}
type tag struct {
customName string
optional bool
skip bool
defaultVal string
note string
}
func parseTag(s string) *tag {
var t tag
escape := false
tokens := []string{""}
for _, r := range s {
if !escape {
switch r {
case '\\':
escape = true
continue
case ',':
tokens = append(tokens, "")
continue
}
}
escape = false
tokens[len(tokens)-1] += string(r)
}
for _, v := range tokens {
switch {
case v == "-":
t.skip = true
case v == "optional":
t.optional = true
case strings.HasPrefix(v, "default="):
t.defaultVal = strings.TrimPrefix(v, "default=")
case strings.HasPrefix(v, "note="):
t.note = strings.TrimPrefix(v, "note=")
default:
t.customName = v
}
}
return &t
}
func readStruct(value reflect.Value, ctx *context) (err error) {
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
name := value.Type().Field(i).Name
tag := parseTag(value.Type().Field(i).Tag.Get("envconfig"))
if tag.skip || !field.CanSet() {
if !field.CanSet() && !ctx.allowUnexported {
return ErrUnexportedField
}
continue
}
doRead:
switch field.Kind() {
case reflect.Ptr:
// it's a pointer, create a new value and restart the switch
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
field = field.Elem()
goto doRead
case reflect.Struct:
err = readStruct(field, &context{
config: ctx.config,
name: ctx.name.Append(name),
optional: ctx.optional || tag.optional,
allowUnexported: ctx.allowUnexported,
})
default:
ctx.config.append(&Field{
name: ctx.name.Append(name),
value: field,
customName: tag.customName,
defaultVal: tag.defaultVal,
note: tag.note,
optional: ctx.optional || tag.optional,
allowUnexported: ctx.allowUnexported,
})
}
if err != nil {
return err
}
}
return err
}
var (
durationType = reflect.TypeOf(new(time.Duration)).Elem()
unmarshalerType = reflect.TypeOf(new(Unmarshaler)).Elem()
)
func isDurationField(t reflect.Type) bool {
return t.AssignableTo(durationType)
}
func isUnmarshaler(t reflect.Type) bool {
return t.Implements(unmarshalerType) || reflect.PtrTo(t).Implements(unmarshalerType)
}
func parseWithUnmarshaler(v reflect.Value, str string) error {
var u = v.Addr().Interface().(Unmarshaler)
return u.Unmarshal(str)
}
func parseDuration(v reflect.Value, str string) error {
d, err := time.ParseDuration(str)
if err != nil {
return err
}
v.SetInt(int64(d))
return nil
}
func parseBoolValue(v reflect.Value, str string) error {
val, err := strconv.ParseBool(str)
if err != nil {
return err
}
v.SetBool(val)
return nil
}
func parseIntValue(v reflect.Value, str string) error {
val, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return err
}
v.SetInt(val)
return nil
}
func parseUintValue(v reflect.Value, str string) error {
val, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return err
}
v.SetUint(val)
return nil
}
func parseFloatValue(v reflect.Value, str string) error {
val, err := strconv.ParseFloat(str, 64)
if err != nil {
return err
}
v.SetFloat(val)
return nil
}
func parseBytesValue(v reflect.Value, str string) error {
val, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return err
}
v.SetBytes(val)
return nil
}