/
parse.go
258 lines (227 loc) · 7.4 KB
/
parse.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
package conf
import (
"fmt"
"io"
"os"
"reflect"
_ "unsafe"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/knadh/koanf/v2"
"github.com/mitchellh/mapstructure"
)
const (
// KeyDelimiter is used as the default key delimiter in the default koanf instance.
KeyDelimiter = "."
)
// NewParser creates a new empty Parser instance.
func NewParser() *Parser {
k := koanf.NewWithConf(koanf.Conf{Delim: KeyDelimiter, StrictMerge: false})
return NewParserFromOperator(k)
}
// NewParserFromFile creates a new Parser by reading the given file.
func NewParserFromFile(fileName string) (*Parser, error) {
// Read yaml config from file.
p := NewParser()
if err := p.k.Load(file.Provider(fileName), yaml.Parser()); err != nil {
return nil, fmt.Errorf("unable to read the file %v: %w", fileName, err)
}
return p, nil
}
// NewParserFromBuffer creates a new Parser by reading the given yaml buffer.
func NewParserFromBuffer(buf io.Reader) (*Parser, error) {
content, err := io.ReadAll(buf)
if err != nil {
return nil, err
}
p := NewParser()
if err := p.k.Load(rawbytes.Provider(content), yaml.Parser()); err != nil {
return nil, err
}
return p, nil
}
// NewParserFromStringMap creates a parser from a map[string]any.
func NewParserFromStringMap(data map[string]any) *Parser {
p := NewParser()
// Cannot return error because the koanf instance is empty.
_ = p.k.Load(confmap.Provider(data, KeyDelimiter), nil)
return p
}
// NewParserFromOperator creates a parser from a koanf.Koanf.
func NewParserFromOperator(k *koanf.Koanf) *Parser {
return &Parser{k: k}
}
func loadFileWithEnv(path string) (koanf.Provider, error) {
fs, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return rawbytes.Provider(ParseEnv(fs)), nil
}
// Parser loads configuration.
type Parser struct {
k *koanf.Koanf
}
// Operator returns the koanf instance.
func (l *Parser) Operator() *koanf.Koanf {
return l.k
}
// AllKeys returns all keys holding a value, regardless of where they are set.
// Nested keys are returned with a KeyDelimiter separator.
func (l *Parser) AllKeys() []string {
return l.k.Keys()
}
// Unmarshal specified path config into a struct.
// Tags on the fields of the structure must be properly set.
func (l *Parser) Unmarshal(key string, dst any) (err error) {
var input any
if key == "" {
input = l.ToStringMap()
} else {
input = l.Get(key)
}
decoder, err := mapstructure.NewDecoder(decoderConfig(dst))
if err != nil {
return err
}
return decoder.Decode(input)
}
// UnmarshalExact unmarshals the config into a struct, error if a field is nonexistent.
func (l *Parser) UnmarshalExact(key string, intoCfg any) (err error) {
var input any
if key == "" {
input = l.ToStringMap()
} else {
input = l.Get(key)
}
dc := decoderConfig(intoCfg)
dc.ErrorUnused = true
decoder, err := mapstructure.NewDecoder(dc)
if err != nil {
return err
}
return decoder.Decode(input)
}
// Get can retrieve any value given the key to use.
func (l *Parser) Get(key string) any {
return l.k.Get(key)
}
// Set sets the value for the key.
func (l *Parser) Set(key string, value any) {
// koanf doesn't offer a direct setting mechanism so merging is required.
merged := koanf.New(KeyDelimiter)
if err := merged.Load(confmap.Provider(map[string]any{key: value}, KeyDelimiter), nil); err != nil {
panic(err)
}
if err := l.k.Merge(merged); err != nil {
panic(err)
}
}
// IsSet checks to see if the key has been set in any of the data locations.
// IsSet is case-insensitive for a key.
func (l *Parser) IsSet(key string) bool {
return l.k.Exists(key)
}
// LoadFileWithEnv loads the given file and env, and merges it into the config.
func (l *Parser) LoadFileWithEnv(path string) error {
provider, err := loadFileWithEnv(path)
if err != nil {
return err
}
return l.k.Load(provider, yaml.Parser())
}
// MergeStringMap merges the configuration from the given map with the existing config.
// Note that the given map may be modified.
func (l *Parser) MergeStringMap(cfg map[string]any) error {
toMerge := koanf.New(KeyDelimiter)
if err := toMerge.Load(confmap.Provider(cfg, KeyDelimiter), nil); err != nil {
return err
}
return l.k.Merge(toMerge)
}
// Sub returns new Parser instance representing a sub-config of this instance.
// It returns an error is the sub-config is not a map (use Get()) or if none exists.
func (l *Parser) Sub(key string) (*Parser, error) {
if !l.IsSet(key) {
return nil, fmt.Errorf("key not exists:%s", key)
}
subParser := NewParser()
subParser.k = l.k.Cut(key)
if len(subParser.ToStringMap()) == 0 {
if l.Get(key) != nil {
return nil, fmt.Errorf("key is not a map")
}
}
return subParser, nil
}
// ToStringMap creates a map[string]any from a Parser.
func (l *Parser) ToStringMap() map[string]any {
return l.k.Raw()
}
// ToBytes takes a Parser implementation and marshals the config map into bytes,
// for example, to TOML or JSON bytes.
func (l *Parser) ToBytes(p koanf.Parser) ([]byte, error) {
return l.k.Marshal(p)
}
//go:linkname textUnmarshalHookFunc github.com/knadh/koanf/v2.textUnmarshalerHookFunc
func textUnmarshalHookFunc() mapstructure.DecodeHookFuncType
// decoderConfig returns a default mapstructure.DecoderConfig capable of parsing time.Duration
// and weakly converting config field values to primitive types. It also ensures that maps
// whose values are nil pointer structs resolved to the zero value of the target struct (see
// expandNilStructPointers). A decoder created from this mapstructure.DecoderConfig will decode
// its contents to the result argument.
func decoderConfig(result any) *mapstructure.DecoderConfig {
return &mapstructure.DecoderConfig{
Result: result,
Metadata: nil,
TagName: "json",
Squash: true,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
expandNilStructPointers(),
textUnmarshalHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
),
}
}
// In cases where a config has a mapping of something to a struct pointers
// we want nil values to resolve to a pointer to the zero value of the
// underlying struct just as we want nil values of a mapping of something
// to a struct to resolve to the zero value of that struct.
//
// e.g. given a config type:
// type Configuration struct { Thing *SomeStruct `mapstructure:"thing"` }
//
// and yaml of:
// config:
//
// thing:
//
// we want an unmarshalled Configuration to be equivalent to
// Configuration{Thing: &SomeStruct{}} instead of Configuration{Thing: nil}
func expandNilStructPointers() mapstructure.DecodeHookFunc {
return func(from reflect.Value, to reflect.Value) (any, error) {
// ensure we are dealing with map to map comparison
if from.Kind() == reflect.Map && to.Kind() == reflect.Map {
toElem := to.Type().Elem()
// ensure that map values are pointers to a struct
// (that may be nil and require manual setting w/ zero value)
if toElem.Kind() == reflect.Ptr && toElem.Elem().Kind() == reflect.Struct {
fromRange := from.MapRange()
for fromRange.Next() {
fromKey := fromRange.Key()
fromValue := fromRange.Value()
// ensure that we've run into a nil pointer instance
if fromValue.IsNil() {
newFromValue := reflect.New(toElem.Elem())
from.SetMapIndex(fromKey, newFromValue)
}
}
}
}
return from.Interface(), nil
}
}