forked from elastic/go-ucfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.go
315 lines (269 loc) · 9.7 KB
/
opts.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
307
308
309
310
311
312
313
314
315
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package ucfg
import (
"fmt"
"os"
"strings"
"github.com/elastic/go-ucfg/parse"
)
// Option type implementing additional options to be passed
// to go-ucfg library functions.
type Option func(*options)
type options struct {
tag string
validatorTag string
pathSep string
meta *Meta
env []*Config
resolvers []func(name string) (string, parse.Config, error)
varexp bool
noParse bool
configValueHandling configHandling
fieldHandlingTree *fieldHandlingTree
// temporary cache of parsed splice values for lifetime of call to
// Unpack/Pack/Get/...
parsed valueCache
activeFields *fieldSet
}
type valueCache map[string]spliceValue
// specific API on top of Config to handle adjusting merging behavior per fields
type fieldHandlingTree Config
// id used to store intermediate parse results in current execution context.
// As parsing results might differ between multiple calls due to:
// splice being shared between multiple configurations, or environment
// changing between calls + lazy nature of cfgSplice, parsing results cannot
// be stored in cfgSplice itself.
type cacheID string
type spliceValue struct {
err error
value value
}
// StructTag option sets the struct tag name to use for looking up
// field names and options in `Unpack` and `Merge`.
// The default struct tag in `config`.
func StructTag(tag string) Option {
return func(o *options) {
o.tag = tag
}
}
// ValidatorTag option sets the struct tag name used to set validators
// on struct fields in `Unpack`.
// The default struct tag in `validate`.
func ValidatorTag(tag string) Option {
return func(o *options) {
o.validatorTag = tag
}
}
// PathSep sets the path separator used to split up names into a tree like hierarchy.
// If PathSep is not set, field names will not be split.
func PathSep(sep string) Option {
return func(o *options) {
o.pathSep = sep
}
}
// MetaData option passes additional metadata (currently only source of the
// configuration) to be stored internally (e.g. for error reporting).
func MetaData(meta Meta) Option {
return func(o *options) {
o.meta = &meta
}
}
// Env option adds another configuration for variable expansion to be used, if
// the path to look up does not exist in the actual configuration. Env can be used
// multiple times in order to add more lookup environments.
func Env(e *Config) Option {
return func(o *options) {
o.env = append(o.env, e)
}
}
// Resolve option adds a callback used by variable name expansion. The callback
// will be called if a variable can not be resolved from within the actual configuration
// or any of its environments.
func Resolve(fn func(name string) (string, parse.Config, error)) Option {
return func(o *options) {
o.resolvers = append(o.resolvers, fn)
}
}
// ResolveEnv option adds a look up callback looking up values in the available
// OS environment variables.
var ResolveEnv Option = doResolveEnv
func doResolveEnv(o *options) {
o.resolvers = append(o.resolvers, func(name string) (string, parse.Config, error) {
value := os.Getenv(name)
if value == "" {
return "", parse.EnvConfig, ErrMissing
}
return value, parse.EnvConfig, nil
})
}
// ResolveNOOP option add a resolver that will not search the value but instead will return the
// provided key wrap with the field reference syntax. This is useful if you don't to expose values
// from envionment variable or other resolvers.
//
// Example: "mysecret" => ${mysecret}"
var ResolveNOOP Option = doResolveNOOP
func doResolveNOOP(o *options) {
o.resolvers = append(o.resolvers, func(name string) (string, parse.Config, error) {
return "${" + name + "}", parse.NoopConfig, nil
})
}
var (
// ReplaceValues option configures all merging and unpacking operations to
// replace old dictionaries and arrays while merging. Value merging can be
// overwritten in unpack by using struct tags.
ReplaceValues = makeOptValueHandling(cfgReplaceValue)
// AppendValues option configures all merging and unpacking operations to
// merge dictionaries and append arrays to existing arrays while merging.
// Value merging can be overwritten in unpack by using struct tags.
AppendValues = makeOptValueHandling(cfgArrAppend)
// PrependValues option configures all merging and unpacking operations to
// merge dictionaries and prepend arrays to existing arrays while merging.
// Value merging can be overwritten in unpack by using struct tags.
PrependValues = makeOptValueHandling(cfgArrPrepend)
)
func makeOptValueHandling(h configHandling) Option {
return func(o *options) {
o.configValueHandling = h
}
}
var (
// FieldMergeValues option configures all merging and unpacking operations to use
// the default merging behavior for the specified field. This overrides the any struct
// tags during unpack for the field. Nested field names can be defined using dot
// notation.
FieldMergeValues = makeFieldOptValueHandling(cfgMergeValues)
// FieldReplaceValues option configures all merging and unpacking operations to
// replace old dictionaries and arrays while merging for the specified field. This
// overrides the any struct tags during unpack for the field. Nested field names
// can be defined using dot notation.
FieldReplaceValues = makeFieldOptValueHandling(cfgReplaceValue)
// FieldAppendValues option configures all merging and unpacking operations to
// merge dictionaries and append arrays to existing arrays while merging for the
// specified field. This overrides the any struct tags during unpack for the field.
// Nested field names can be defined using dot notation.
FieldAppendValues = makeFieldOptValueHandling(cfgArrAppend)
// FieldPrependValues option configures all merging and unpacking operations to
// merge dictionaries and prepend arrays to existing arrays while merging for the
// specified field. This overrides the any struct tags during unpack for the field.
// Nested field names can be defined using dot notation.
FieldPrependValues = makeFieldOptValueHandling(cfgArrPrepend)
)
func makeFieldOptValueHandling(h configHandling) func(...string) Option {
return func(fieldName ...string) Option {
if len(fieldName) == 0 {
return func(_ *options) {}
}
table := make(map[string]configHandling)
for _, name := range fieldName {
// field value config options are rendered into a Config; the '*' represents the handling method
// for everything nested under this field.
if !strings.HasSuffix(name, ".*") {
name = fmt.Sprintf("%s.*", name)
}
table[name] = h
}
return func(o *options) {
if o.fieldHandlingTree == nil {
o.fieldHandlingTree = newFieldHandlingTree()
}
o.fieldHandlingTree.merge(table, PathSep(o.pathSep))
}
}
}
// VarExp option enables support for variable expansion. Resolve and Env options will only be effective if VarExp is set.
var VarExp Option = doVarExp
func doVarExp(o *options) { o.varexp = true }
func makeOptions(opts []Option) *options {
o := options{
tag: "config",
validatorTag: "validate",
pathSep: "", // no separator by default
parsed: map[string]spliceValue{},
activeFields: newFieldSet(nil),
}
for _, opt := range opts {
opt(&o)
}
return &o
}
func (cache valueCache) cachedValue(
id cacheID,
f func() (value, error),
) (value, error) {
if v, ok := cache[string(id)]; ok {
if v.err != nil {
return nil, v.err
}
return v.value, nil
}
v, err := f()
// Only primitives can be cached, allowing us to get out of infinite loop
if v != nil && v.canCache() {
cache[string(id)] = spliceValue{err, v}
}
return v, err
}
func newFieldHandlingTree() *fieldHandlingTree {
return (*fieldHandlingTree)(New())
}
func (t *fieldHandlingTree) merge(other interface{}, opts ...Option) error {
cfg := (*Config)(t)
return cfg.Merge(other, opts...)
}
func (t *fieldHandlingTree) child(fieldName string, idx int) (*fieldHandlingTree, error) {
cfg := (*Config)(t)
child, err := cfg.Child(fieldName, idx)
if err != nil {
return nil, err
}
return (*fieldHandlingTree)(child), nil
}
func (t *fieldHandlingTree) configHandling(fieldName string, idx int) (configHandling, error) {
cfg := (*Config)(t)
handling, err := cfg.Uint(fieldName, idx)
if err != nil {
return cfgDefaultHandling, err
}
return configHandling(handling), nil
}
func (t *fieldHandlingTree) wildcard() (*fieldHandlingTree, error) {
return t.child("**", -1)
}
func (t *fieldHandlingTree) setWildcard(wildcard *fieldHandlingTree) error {
cfg := (*Config)(t)
return cfg.SetChild("**", -1, (*Config)(wildcard))
}
func (t *fieldHandlingTree) fieldHandling(fieldName string, idx int) (configHandling, *fieldHandlingTree, bool) {
child, err := t.child(fieldName, idx)
if err == nil {
cfgHandling, err := child.configHandling("*", -1)
if err == nil {
return cfgHandling, child, true
}
}
// try wildcard match
wildcard, err := t.wildcard()
if err != nil {
return cfgDefaultHandling, child, false
}
cfgHandling, cfg, ok := wildcard.fieldHandling(fieldName, idx)
if ok {
return cfgHandling, cfg, ok
}
return cfgDefaultHandling, child, ok
}