-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.go
254 lines (231 loc) · 6.88 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
package copre
import (
"encoding/base64"
"encoding/hex"
"fmt"
"net"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/spf13/pflag"
)
type envOptions struct {
tag string
prefix string
keyGetter func([]string) string
}
// EnvOption configures how environment variables are used to populate a given structure.
type EnvOption interface {
apply(*envOptions)
}
type envOptionAdapter func(*envOptions)
func (c envOptionAdapter) apply(o *envOptions) {
c(o)
}
// WithPrefix will prefix environment variable names with the specified value unless noprefix option is set in the tag.
func WithPrefix(prefix string) EnvOption {
return envOptionAdapter(func(o *envOptions) {
o.prefix = prefix
})
}
// ComputeEnvKey will remove the requirement to explicitly specify the
// env-key with a tag. For all fields not explicitly tagged, the name
// will be computed based on the path by the provided nameGetter function.
// For example:
// ComputeEnvKey(UpperSnakeCase)
func ComputeEnvKey(keyGetter func([]string) string) EnvOption {
return envOptionAdapter(func(o *envOptions) {
o.keyGetter = keyGetter
})
}
// OverrideEnvTag will change the struct-tag used to retrieve the env-key and options.
// The main purpose of this option is to allow interoperability with libraries
// using the same tag.
//
// However it can also be used to enable edge-cases where multiple sets of environment
// variables are used to populate the same field.
func OverrideEnvTag(tag string) EnvOption {
return envOptionAdapter(func(o *envOptions) {
if tag == "" {
tag = "env"
}
o.tag = tag
})
}
// Env implements a Loader, that uses environment variables to retrieve
// configuration values.
//
// Standalone usage example:
// cfg := struct{ // Illustrating some ways to load bytes from env
// A []byte `env:"NOPREFIX_A,noprefix"`
// B []byte `env:"MYPREFIX_B,hex"`
// C []byte `env:",base64"`
// }{}
// err := Env(WithPrefix("MYPREFIX"), ComputeEnvKey(UpperSnakeCase)).Process(&cfg)
func Env(opts ...EnvOption) Loader {
o := envOptions{
tag: "env",
prefix: "",
keyGetter: func(s []string) string { return "" },
}
for _, opt := range opts {
opt.apply(&o)
}
return LoaderFunc(func(dst interface{}) error {
return StructWalk(dst, func(path []string, field reflect.StructField) (interface{}, error) {
noPrefix := false
key := o.keyGetter(path)
targetType := field.Type
if tag, ok := field.Tag.Lookup(o.tag); ok {
params := strings.Split(tag, ",")
// Only set key if provided
if params[0] != "" {
key = params[0]
}
if len(params) > 1 { // If options are set, let's handle them
for _, param := range params[1:] {
// Check options for byte arrays
if param == "hex" || param == "base64" {
if targetType.Kind() != reflect.Slice || targetType.Elem().Kind() != reflect.Uint8 {
return nil, fmt.Errorf("unsupported option '%s' for type '%s'", params[1], targetType.String())
}
if param == "hex" {
targetType = reflect.TypeOf(convertBytesHexMarker{})
} else if param == "base64" {
targetType = reflect.TypeOf(convertBytesBase64Marker{})
}
}
if param == "noprefix" {
noPrefix = true
}
}
}
}
if o.prefix != "" && !noPrefix {
key = fmt.Sprintf("%s_%s", o.prefix, key)
}
if val, ok := os.LookupEnv(key); ok {
return convertString(targetType, val)
}
return nil, nil
})
})
}
const (
arrayDelimiter = ","
mapDelimiter = ","
mapKVDelimiter = "="
)
type convertBytesHexMarker []byte
type convertBytesBase64Marker []byte
// Converts `input` string to type `t` or returns error if operation is not
// possible. Type `t` needs to be NOT a pointer kind!
func convertString(t reflect.Type, input string) (interface{}, error) {
if t.Kind() == reflect.Ptr {
return nil, fmt.Errorf("no pointer kinds allowed")
}
// Let's handle the marker structs
if strings.Contains(t.PkgPath(), "copre") {
switch t.Name() {
case "convertBytesHexMarker":
return hex.DecodeString(input)
case "convertBytesBase64Marker":
return base64.StdEncoding.DecodeString(input)
}
}
// Handle supported net-types
if t.PkgPath() == "net" {
switch t.Name() {
case "IP":
ip := net.ParseIP(input)
if ip == nil {
return nil, fmt.Errorf("unable to parse '%s' as net.IP", input)
}
return ip, nil
case "IPMask":
ipMask := pflag.ParseIPv4Mask(input)
if ipMask == nil {
return nil, fmt.Errorf("unable to parse '%s' as net.IPMast", input)
}
return ipMask, nil
case "IPNet":
ip, ipNet, err := net.ParseCIDR(input)
if err != nil {
return nil, fmt.Errorf("unable to parse '%s' as net.IPNet, failed with: %w", input, err)
}
ipNet.IP = ip
return *ipNet, nil
}
}
// Parse time.Duration
if t.PkgPath() == "time" && t.Name() == "Duration" {
d, err := time.ParseDuration(input)
if err != nil {
return nil, fmt.Errorf("unable to parse '%s' as time.Duration, failed with: %w", input, err)
}
return d, nil
}
// Convert to in-built types
var (
v interface{}
err error
)
switch t.Kind() {
case reflect.String:
v = input
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err = strconv.ParseInt(input, 0, t.Bits())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err = strconv.ParseUint(input, 0, t.Bits())
case reflect.Bool:
v, err = strconv.ParseBool(input)
case reflect.Float32, reflect.Float64:
v, err = strconv.ParseFloat(input, t.Bits())
case reflect.Slice:
elems := strings.Split(input, arrayDelimiter)
values := reflect.MakeSlice(t, len(elems), len(elems))
for i, elem := range elems {
convertedValue, err := convertString(t.Elem(), elem)
if err != nil {
return nil, err
}
values.Index(i).Set(reflect.ValueOf(convertedValue))
}
return values.Interface(), nil
case reflect.Map:
values := reflect.MakeMap(t)
keyValues := strings.Split(input, mapDelimiter)
for _, keyValueUnsplit := range keyValues {
keyValue := strings.Split(keyValueUnsplit, mapKVDelimiter)
if len(keyValue) != 2 {
return nil, fmt.Errorf("invalid key value item provided: %s", keyValueUnsplit)
}
key := reflect.New(t.Key()).Elem()
keyData, err := convertString(key.Type(), keyValue[0])
if err != nil {
return nil, err
}
key.Set(reflect.ValueOf(keyData))
value := reflect.New(t.Elem()).Elem()
valueData, err := convertString(value.Type(), keyValue[1])
if err != nil {
return nil, err
}
value.Set(reflect.ValueOf(valueData))
values.SetMapIndex(key, value)
}
return values.Interface(), nil
default:
return nil, fmt.Errorf("unsupported type: %s", t.Kind().String())
}
if err != nil {
return nil, err
}
// `strconv` functions always return largest type, so the primary purpose of
// this function is to convert them using reflection, e.g. int64 -> int32
vv := reflect.ValueOf(v)
vv = vv.Convert(t)
return vv.Interface(), nil
}