forked from elastic/go-ucfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
326 lines (270 loc) · 7.12 KB
/
path.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
316
317
318
319
320
321
322
323
324
325
326
// 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"
"strconv"
"strings"
)
type cfgPath struct {
fields []field
sep string
}
type field interface {
String() string
SetValue(opt *options, elem value, v value) Error
GetValue(opt *options, elem value) (value, Error)
Remove(opt *options, elem value) (bool, Error)
}
type namedField struct {
name string
}
type idxField struct {
i int
}
func parsePathIdx(in string, idx int, opts *options) cfgPath {
if in == "" {
return cfgPath{
sep: opts.pathSep,
fields: []field{idxField{idx}},
}
}
p := parsePathWithOpts(in, opts)
if idx >= 0 {
p.fields = append(p.fields, idxField{idx})
}
return p
}
func parsePath(in, sep string, maxIdx int64, enableNumKeys bool) cfgPath {
if sep == "" {
return cfgPath{
sep: sep,
fields: []field{parseField(in, maxIdx, enableNumKeys)},
}
}
elems := strings.Split(in, sep)
fields := make([]field, 0, len(elems))
// If property is the name with separators, for example "inputs.0.i"
// fall back to original implementation
if len(elems) > 1 {
enableNumKeys = false
}
for _, elem := range elems {
fields = append(fields, parseField(elem, maxIdx, enableNumKeys))
}
return cfgPath{fields: fields, sep: sep}
}
func parsePathWithOpts(in string, opts *options) cfgPath {
return parsePath(in, opts.pathSep, opts.maxIdx, opts.enableNumKeys)
}
func parseField(in string, maxIdx int64, enableNumKeys bool) field {
// If numeric keys are not enabled, fallback to the original implementation
if !enableNumKeys {
idx, err := strconv.ParseInt(in, 0, 64)
// Limit index value to the configurable max.
// If the idx > opts.maxIdx treat it as a regular named field.
// This preserves the current behavour for small index fields values (<= opts.maxIdx)
// and prevents large memory allocations or OOM if the string is large numeric value
if err == nil && idx <= int64(maxIdx) {
return idxField{int(idx)}
}
}
return namedField{in}
}
func (p cfgPath) String() string {
if len(p.fields) == 0 {
return ""
}
if len(p.fields) == 1 {
return p.fields[0].String()
}
s := make([]string, 0, len(p.fields))
for _, f := range p.fields {
s = append(s, f.String())
}
sep := p.sep
if sep == "" {
sep = "."
}
return strings.Join(s, sep)
}
func (n namedField) String() string {
return n.name
}
func (i idxField) String() string {
return fmt.Sprintf("%d", i.i)
}
func (p cfgPath) Has(cfg *Config, opt *options) (bool, Error) {
fields := p.fields
cur := value(cfgSub{cfg})
for ; len(fields) > 0; fields = fields[1:] {
field := fields[0]
next, err := field.GetValue(opt, cur)
if err != nil {
// has checks if a value is missing -> ErrMissing is no error but a valid
// outcome
if err.Reason() == ErrMissing {
err = nil
}
return false, err
}
if next == nil {
return false, nil
}
cur = next
}
return true, nil
}
func (p cfgPath) GetValue(cfg *Config, opt *options) (value, Error) {
fields := p.fields
cur := value(cfgSub{cfg})
for ; len(fields) > 1; fields = fields[1:] {
field := fields[0]
next, err := field.GetValue(opt, cur)
if err != nil {
return nil, err
}
if next == nil {
return nil, raiseMissing(cfg, field.String())
}
cur = next
}
field := fields[0]
v, err := field.GetValue(opt, cur)
if err != nil {
return nil, raiseMissing(cfg, field.String())
}
return v, nil
}
func (n namedField) GetValue(opts *options, elem value) (value, Error) {
cfg, err := elem.toConfig(opts)
if err != nil {
return nil, raiseExpectedObject(opts, elem)
}
v, _ := cfg.fields.get(n.name)
return v, nil
}
func (i idxField) GetValue(opts *options, elem value) (value, Error) {
cfg, err := elem.toConfig(opts)
if err != nil {
if i.i == 0 {
return elem, nil
}
return nil, raiseExpectedObject(opts, elem)
}
arr := cfg.fields.array()
if i.i >= len(arr) {
return nil, raiseMissing(cfg, i.String())
}
return arr[i.i], nil
}
func (p cfgPath) SetValue(cfg *Config, opt *options, val value) Error {
fields := p.fields
node := value(cfgSub{cfg})
// 1. iterate until intermediate node not having some required child node
for ; len(fields) > 1; fields = fields[1:] {
field := fields[0]
v, err := field.GetValue(opt, node)
if err != nil {
if err.Reason() == ErrMissing {
break
}
return err
}
if isNil(v) {
break
}
node = v
}
// 2. build intermediate nodes from bottom up
for ; len(fields) > 1; fields = fields[:len(fields)-1] {
field := fields[len(fields)-1]
next := New()
next.metadata = val.meta()
v := cfgSub{next}
if err := field.SetValue(opt, v, val); err != nil {
return err
}
val = v
}
// 3. insert new sub-tree into config
return fields[0].SetValue(opt, node, val)
}
func (n namedField) SetValue(opts *options, elem value, v value) Error {
sub, ok := elem.(cfgSub)
if !ok {
return raiseExpectedObject(opts, elem)
}
sub.c.fields.set(n.name, v)
v.SetContext(context{parent: elem, field: n.name})
return nil
}
func (i idxField) SetValue(opts *options, elem value, v value) Error {
sub, ok := elem.(cfgSub)
if !ok {
return raiseExpectedObject(opts, elem)
}
sub.c.fields.setAt(i.i, elem, v)
v.SetContext(context{parent: elem, field: i.String()})
return nil
}
func (p cfgPath) Remove(cfg *Config, opt *options) (bool, error) {
fields := p.fields
// Loop over intermediate objects. Returns an error if any intermediate is
// actually no object.
cur := value(cfgSub{cfg})
for ; len(fields) > 1; fields = fields[1:] {
field := fields[0]
next, err := field.GetValue(opt, cur)
if err != nil {
// Ignore ErrMissing when walking down a config tree. If intermediary is
// missing we can't remove our setting.
if err.Reason() == ErrMissing {
err = nil
}
return false, err
}
if next == nil {
return false, err
}
cur = next
}
// resolve config object in case we deal with references
tmp, err := cur.toConfig(opt)
if err != nil {
return false, err
}
cur = cfgSub{tmp}
field := fields[0]
return field.Remove(opt, cur)
}
func (n namedField) Remove(opts *options, elem value) (bool, Error) {
sub, ok := elem.(cfgSub)
if !ok {
return false, raiseExpectedObject(opts, elem)
}
removed := sub.c.fields.del(n.name)
return removed, nil
}
func (i idxField) Remove(opts *options, elem value) (bool, Error) {
sub, ok := elem.(cfgSub)
if !ok {
return false, raiseExpectedObject(opts, elem)
}
removed := sub.c.fields.delAt(i.i)
return removed, nil
}