-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
221 lines (182 loc) · 4.87 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
package goenv
import (
"errors"
"fmt"
"os"
"reflect"
"strconv"
"log"
"github.com/joho/godotenv"
)
type Config struct {
EnvTag string
DefaultValueTag string
RequiredValueTag string
}
type ParserFunc func(value string) (interface{}, error)
var (
// ErrNoStruct error when given parametetr is not struct
ErrNoStruct = errors.New("given parameter is not a struct")
// ErrNoPtr error when given parameter is not pointer
ErrNoPtr = errors.New("given parameter is not a pointer")
// DefaultTag tag for loading env key names from struct
DefaultTag = "env"
// Default value of env variable
DefaultValueTag = "default"
// Required env variable
DefaultRequiredTag = "required"
// Built-in parser functions
BuiltInParsers = map[reflect.Kind]ParserFunc{
reflect.Bool: func(value string) (interface{}, error) {
return strconv.ParseBool(value)
},
reflect.Int: func(value string) (interface{}, error) {
return strconv.Atoi(value)
},
reflect.Int8: func(value string) (interface{}, error) {
val, err := strconv.ParseInt(value, 10, 8)
if err != nil {
return nil, err
}
return int8(val), nil
},
reflect.Int16: func(value string) (interface{}, error) {
val, err := strconv.ParseInt(value, 10, 16)
if err != nil {
return nil, err
}
return int16(val), nil
},
reflect.Int32: func(value string) (interface{}, error) {
val, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return nil, err
}
return int32(val), nil
},
reflect.Int64: func(value string) (interface{}, error) {
return strconv.ParseInt(value, 10, 64)
},
reflect.Uint: func(value string) (interface{}, error) {
val, err := strconv.ParseUint(value, 10, 0)
if err != nil {
return nil, err
}
return uint(val), nil
},
reflect.Uint8: func(value string) (interface{}, error) {
val, err := strconv.ParseUint(value, 10, 8)
if err != nil {
return nil, err
}
return uint8(val), nil
},
reflect.Uint16: func(value string) (interface{}, error) {
val, err := strconv.ParseUint(value, 10, 16)
if err != nil {
return nil, err
}
return uint16(val), nil
},
reflect.Uint32: func(value string) (interface{}, error) {
val, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return nil, err
}
return uint32(val), nil
},
reflect.Uint64: func(value string) (interface{}, error) {
return strconv.ParseUint(value, 10, 64)
},
reflect.Float32: func(value string) (interface{}, error) {
val, err := strconv.ParseFloat(value, 32)
if err != nil {
return nil, err
}
return float32(val), nil
},
reflect.Float64: func(value string) (interface{}, error) {
return strconv.ParseFloat(value, 64)
},
reflect.String: func(value string) (interface{}, error) {
return value, nil
},
}
)
func New(envFiles ...string) (Config, error) {
if err := godotenv.Load(envFiles...); err != nil {
log.Println(err)
}
return Config{
EnvTag: DefaultTag,
DefaultValueTag: DefaultValueTag,
RequiredValueTag: DefaultRequiredTag,
}, nil
}
func (e Config) Parse(s interface{}, data map[string]string) error {
reflectValue := reflect.ValueOf(s)
if reflectValue.Kind() != reflect.Ptr || reflectValue.IsNil() {
return ErrNoPtr
}
reflectValue = reflectValue.Elem()
if reflectValue.Kind() != reflect.Struct {
return ErrNoStruct
}
t := reflectValue.Type()
for i := 0; i < reflectValue.NumField(); i++ {
valueField := reflectValue.Field(i)
if valueField.Kind() == reflect.Struct {
if !valueField.Addr().CanInterface() {
continue
}
iFace := valueField.Addr().Interface()
if err := e.Parse(iFace, data); err != nil {
return err
}
}
typeField := t.Field(i)
parsedValue, err := e.parseValue(typeField, data)
if err != nil {
return err
}
if parsedValue != nil {
reflectValue.Field(i).Set(reflect.ValueOf(parsedValue))
}
}
return nil
}
func (e Config) parseValue(typeField reflect.StructField, data map[string]string) (interface{}, error) {
key, defaultValue := typeField.Tag.Get(e.EnvTag), typeField.Tag.Get(e.DefaultValueTag)
isRequired := typeField.Tag.Get(e.RequiredValueTag) == "true"
if key != "" {
var value string
if data != nil {
value = e.getOrDefaultMap(key, defaultValue, data)
}
if value == "" {
value = e.getOrDefault(key, defaultValue)
}
parser := BuiltInParsers[typeField.Type.Kind()]
parsedValue, err := parser(value)
if err != nil {
return nil, err
}
if parsedValue == "" && isRequired {
return nil, fmt.Errorf("%s required", key)
}
return parsedValue, nil
}
return nil, nil
}
func (e Config) getOrDefaultMap(key string, defaultValue string, data map[string]string) string {
if v, ok := data[key]; ok {
return string(v)
}
return defaultValue
}
func (e Config) getOrDefault(key string, defaultValue string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
return defaultValue
}