-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_field.go
278 lines (246 loc) · 5.78 KB
/
config_field.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
package envconf
import (
"flag"
"os"
"reflect"
"strconv"
"strings"
"github.com/antonmashko/envconf/option"
)
const (
tagFlag = "flag"
tagEnv = "env"
tagDefault = "default"
tagRequired = "required"
tagDescription = "description"
tagIgnored = "-"
tagNotDefined = ""
valIgnored = "ignored"
valNotDefined = "N/D"
valDefault = "*"
)
type flagSource struct {
name string
v string
defined bool
}
func newFlagSource(f *configField, tag reflect.StructField, usage string) *flagSource {
name, ok := tag.Tag.Lookup(tagFlag)
if !ok || name == tagNotDefined {
name = tagIgnored
} else if name == valDefault {
// generating flag name
const flagDelim = "-"
name = strings.ToLower(fullname(f, flagDelim))
}
fs := &flagSource{
name: name,
}
if name != tagIgnored {
flag.Var(fs, name, usage)
}
return fs
}
func (s *flagSource) Name() string {
return s.name
}
func (s *flagSource) Value() (interface{}, option.ConfigSource) {
if s.name == tagIgnored {
return "", option.NoConfigValue
}
if !s.defined {
return "", option.NoConfigValue
}
return s.v, option.FlagVariable
}
func (s *flagSource) Set(value string) error {
s.v = value
s.defined = true
return nil
}
func (s *flagSource) String() string {
return s.v
}
type envSource struct {
name string
}
func newEnvSource(f *configField, tag reflect.StructField) *envSource {
name, ok := tag.Tag.Lookup(tagEnv)
if !ok || name == tagNotDefined {
name = tagIgnored
} else if name == valDefault {
// generating env var name
const envDelim = "_"
name = strings.ToUpper(fullname(f, envDelim))
}
return &envSource{
name: name,
}
}
func (s *envSource) Name() string {
return s.name
}
func (s *envSource) Value() (interface{}, option.ConfigSource) {
if s.name == tagIgnored {
return "", option.NoConfigValue
}
v, ok := os.LookupEnv(s.name)
if !ok {
return "", option.NoConfigValue
}
return v, option.EnvVariable
}
type externalSource struct {
f field
opts *option.Options
}
func newExternalSource(f field, opts *option.Options) *externalSource {
return &externalSource{
f: f,
opts: opts,
}
}
func (s *externalSource) Value() (interface{}, option.ConfigSource) {
if s.f.parent() == nil {
return nil, option.NoConfigValue
}
v, ok := s.f.parent().externalSource().
Read(s.f.structField().Name)
if !ok {
return nil, option.NoConfigValue
}
envInjF := s.opts.ExternalInjection()
if envInjF == nil {
return v, option.ExternalSource
}
str, ok := v.(string)
if !ok {
return v, option.ExternalSource
}
var cs option.ConfigSource
str, cs = envInjF(str)
switch cs {
case option.EnvVariable:
v, cs = (&envSource{name: str}).Value()
if cs == option.NoConfigValue {
return nil, option.NoConfigValue
}
return v, option.EnvVariable
default:
return v, option.ExternalSource
}
}
type defaultValueSource struct {
defined bool
v string
}
func newDefaultValueSource(tag reflect.StructField) *defaultValueSource {
var s defaultValueSource
s.v, s.defined = tag.Tag.Lookup(tagDefault)
return &s
}
func (s *defaultValueSource) Value() (interface{}, option.ConfigSource) {
if !s.defined {
return nil, option.NoConfigValue
}
return s.v, option.DefaultValue
}
type configField struct {
reflect.StructField
parentField field
parser *EnvConf
configuration struct {
flag *flagSource
env *envSource
external *externalSource
defaultValue *defaultValueSource
}
property struct {
required bool
description string
}
value interface{}
source option.ConfigSource
}
func newConfigField(parent field, sf reflect.StructField, parser *EnvConf) *configField {
return newDefinedConfigField(nil, option.NoConfigValue, parent, sf, parser)
}
func newDefinedConfigField(v interface{}, cs option.ConfigSource, parent field, sf reflect.StructField, parser *EnvConf) *configField {
return &configField{
StructField: sf,
parentField: parent,
parser: parser,
value: v,
source: cs,
}
}
// initialize setting for specific field
func (f *configField) init(fl field) error {
req, ok := f.Tag.Lookup(tagRequired)
if ok {
var err error
f.property.required, err = strconv.ParseBool(req)
if err != nil {
return err
}
}
f.property.description = f.Tag.Get(tagDescription)
f.configuration.flag = newFlagSource(f, f.StructField, f.property.description)
f.configuration.env = newEnvSource(f, f.StructField)
f.configuration.external = newExternalSource(fl, f.parser.opts)
f.configuration.defaultValue = newDefaultValueSource(f.StructField)
return nil
}
func (f *configField) name() string {
return f.Name
}
func (f *configField) fullName() string {
return fullname(f, fieldNameDelim)
}
func (f *configField) parent() field {
if f.parentField == nil {
return nil
}
return f.parentField
}
func (f *configField) structField() reflect.StructField {
return f.StructField
}
func (f *configField) IsRequired() bool {
return f.property.required
}
func (f *configField) isSet() bool {
return f.value != nil && f.source != option.NoConfigValue
}
func (f *configField) set(v interface{}, cs option.ConfigSource) error {
f.value = v
f.source = cs
return nil
}
func (f *configField) Value() (interface{}, option.ConfigSource) {
if f.isSet() {
return f.value, f.source
}
priority := f.parser.PriorityOrder()
for _, p := range priority {
var confF func() (interface{}, option.ConfigSource) = nil
switch p {
case option.FlagVariable:
confF = f.configuration.flag.Value
case option.EnvVariable:
confF = f.configuration.env.Value
case option.ExternalSource:
confF = f.configuration.external.Value
case option.DefaultValue:
confF = f.configuration.defaultValue.Value
}
if confF == nil {
continue
}
v, cs := confF()
if cs != option.NoConfigValue {
return v, cs
}
}
return nil, option.NoConfigValue
}