forked from elastic/go-ucfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getset.go
281 lines (258 loc) · 10.8 KB
/
getset.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
// 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
// ******************************************************************************
// Low level getters and setters
// ******************************************************************************
func convertErr(opts *options, v value, err error, to string) Error {
if err == nil {
return nil
}
return raiseConversion(opts, v, err, to)
}
// CountField returns number of entries in a table or 1 if entry is a primitive value.
// Primitives settings can be handled like a list with 1 entry.
//
// If name is empty, the total number of top-level settings is returned.
//
// CountField supports the options: PathSep, Env, Resolve, ResolveEnv
func (c *Config) CountField(name string, opts ...Option) (int, error) {
if name == "" {
return len(c.fields.array()) + len(c.fields.dict()), nil
}
if v, ok := c.fields.get(name); ok {
return v.Len(makeOptions(opts))
}
return -1, raiseMissing(c, name)
}
// Bool reads a boolean setting returning an error if the setting has no
// boolean value.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// Bool supports the options: PathSep, Env, Resolve, ResolveEnv
func (c *Config) Bool(name string, idx int, opts ...Option) (bool, error) {
O := makeOptions(opts)
v, err := c.getField(name, idx, O)
if err != nil {
return false, err
}
b, fail := v.toBool(O)
return b, convertErr(O, v, fail, "bool")
}
// Strings reads a string setting returning an error if the setting has
// no string or primitive value convertible to string.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// String supports the options: PathSep, Env, Resolve, ResolveEnv
func (c *Config) String(name string, idx int, opts ...Option) (string, error) {
O := makeOptions(opts)
v, err := c.getField(name, idx, O)
if err != nil {
return "", err
}
s, fail := v.toString(O)
return s, convertErr(O, v, fail, "string")
}
// Int reads an int64 value returning an error if the setting is
// not integer value, the primitive value is not convertible to int or a conversion
// would create an integer overflow.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// Int supports the options: PathSep, Env, Resolve, ResolveEnv
func (c *Config) Int(name string, idx int, opts ...Option) (int64, error) {
O := makeOptions(opts)
v, err := c.getField(name, idx, O)
if err != nil {
return 0, err
}
i, fail := v.toInt(O)
return i, convertErr(O, v, fail, "int")
}
// Uint reads an uint64 value returning an error if the setting is
// not unsigned value, the primitive value is not convertible to uint64 or a conversion
// would create an integer overflow.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// Uint supports the options: PathSep, Env, Resolve, ResolveEnv
func (c *Config) Uint(name string, idx int, opts ...Option) (uint64, error) {
O := makeOptions(opts)
v, err := c.getField(name, idx, O)
if err != nil {
return 0, err
}
u, fail := v.toUint(O)
return u, convertErr(O, v, fail, "uint")
}
// Float reads a float64 value returning an error if the setting is
// not a float value or the primitive value is not convertible to float.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// Float supports the options: PathSep, Env, Resolve, ResolveEnv
func (c *Config) Float(name string, idx int, opts ...Option) (float64, error) {
O := makeOptions(opts)
v, err := c.getField(name, idx, O)
if err != nil {
return 0, err
}
f, fail := v.toFloat(O)
return f, convertErr(O, v, fail, "float")
}
// Child returns a child configuration or an error if the setting requested is a
// primitive value only.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// Child supports the options: PathSep, Env, Resolve, ResolveEnv
func (c *Config) Child(name string, idx int, opts ...Option) (*Config, error) {
O := makeOptions(opts)
v, err := c.getField(name, idx, O)
if err != nil {
return nil, err
}
c, fail := v.toConfig(O)
return c, convertErr(O, v, fail, "object")
}
// SetBool sets a boolean primitive value. An error is returned if the new name
// is invalid.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// SetBool supports the options: PathSep, MetaData
func (c *Config) SetBool(name string, idx int, value bool, opts ...Option) error {
return c.setField(name, idx, &cfgBool{b: value}, opts)
}
// SetInt sets an integer primitive value. An error is returned if the new
// name is invalid.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// SetInt supports the options: PathSep, MetaData
func (c *Config) SetInt(name string, idx int, value int64, opts ...Option) error {
return c.setField(name, idx, &cfgInt{i: value}, opts)
}
// SetUint sets an unsigned integer primitive value. An error is returned if
// the name is invalid.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// SetUint supports the options: PathSep, MetaData
func (c *Config) SetUint(name string, idx int, value uint64, opts ...Option) error {
return c.setField(name, idx, &cfgUint{u: value}, opts)
}
// SetFloat sets an floating point primitive value. An error is returned if
// the name is invalid.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// SetFloat supports the options: PathSep, MetaData
func (c *Config) SetFloat(name string, idx int, value float64, opts ...Option) error {
return c.setField(name, idx, &cfgFloat{f: value}, opts)
}
// SetString sets string value. An error is returned if the name is invalid.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// SetString supports the options: PathSep, MetaData
func (c *Config) SetString(name string, idx int, value string, opts ...Option) error {
return c.setField(name, idx, &cfgString{s: value}, opts)
}
// SetChild adds a sub-configuration. An error is returned if the name is invalid.
//
// The setting path is constructed from name and idx. If name is set and idx is -1,
// only the name is used to access the setting by name. If name is empty, idx
// must be >= 0, assuming the Config is a list. If both name and idx are set,
// the name must point to a list. The number of entries in a named list can be read
// using CountField.
//
// SetChild supports the options: PathSep, MetaData
func (c *Config) SetChild(name string, idx int, value *Config, opts ...Option) error {
return c.setField(name, idx, cfgSub{c: value}, opts)
}
// getField supports the options: PathSep, Env, Resolve, ResolveEnv
func (c *Config) getField(name string, idx int, opts *options) (value, Error) {
p := parsePathIdx(name, idx, opts)
v, err := p.GetValue(c, opts)
if err != nil {
return v, err
}
if v == nil {
return nil, raiseMissing(c, p.String())
}
return v, nil
}
// setField supports the options: PathSep, MetaData
func (c *Config) setField(name string, idx int, v value, options []Option) Error {
opts := makeOptions(options)
p := parsePathIdx(name, idx, opts)
err := p.SetValue(c, opts, v)
if err != nil {
return err
}
if opts.meta != nil {
v.setMeta(opts.meta)
}
return nil
}