-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_config_test.go
More file actions
423 lines (334 loc) · 9.89 KB
/
example_config_test.go
File metadata and controls
423 lines (334 loc) · 9.89 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package config_test
import (
"context"
"fmt"
"sort"
"github.com/tarantool/go-config"
"github.com/tarantool/go-config/collectors"
)
// Example_basicGetAndLookup demonstrates the core Config API methods:
// Get (extracts a typed value), Lookup (returns a Value without error on miss),
// and Stat (returns only metadata without touching the value).
func Example_basicGetAndLookup() {
data := map[string]any{
"server": map[string]any{
"host": "localhost",
"port": 8080,
},
}
builder := config.NewBuilder()
builder = builder.AddCollector(
collectors.NewMap(data).
WithName("app-config").
WithSourceType(config.FileSource).
WithRevision("v1.0"),
)
cfg, errs := builder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
// Get extracts a typed value and returns metadata.
var host string
meta, err := cfg.Get(config.NewKeyPath("server/host"), &host)
if err != nil {
fmt.Printf("Get error: %v\n", err)
return
}
fmt.Printf("Host: %s\n", host)
fmt.Printf("Source: %s\n", meta.Source.Name)
fmt.Printf("Revision: %s\n", meta.Revision)
// Get returns an error for missing keys.
var missing string
_, err = cfg.Get(config.NewKeyPath("server/timeout"), &missing)
fmt.Printf("Missing key error: %v\n", err)
// Lookup returns (Value, bool) — no error on miss.
val, ok := cfg.Lookup(config.NewKeyPath("server/port"))
fmt.Printf("Port found: %v\n", ok)
if ok {
var port int
_ = val.Get(&port)
fmt.Printf("Port: %d\n", port)
}
_, ok = cfg.Lookup(config.NewKeyPath("server/missing"))
fmt.Printf("Missing found: %v\n", ok)
// Stat returns metadata without extracting the value.
statMeta, ok := cfg.Stat(config.NewKeyPath("server/host"))
fmt.Printf("Stat found: %v\n", ok)
fmt.Printf("Stat source: %s\n", statMeta.Source.Name)
// Output:
// Host: localhost
// Source: app-config
// Revision: v1.0
// Missing key error: key not found: server/timeout
// Port found: true
// Port: 8080
// Missing found: false
// Stat found: true
// Stat source: app-config
}
// Example_walkConfig demonstrates Config.Walk() for iterating over all
// leaf values in the configuration tree with optional depth control.
func Example_walkConfig() {
data := map[string]any{
"database": map[string]any{
"host": "localhost",
"port": 5432,
"pool": map[string]any{
"max_size": 10,
},
},
}
builder := config.NewBuilder()
builder = builder.AddCollector(collectors.NewMap(data).WithName("config"))
cfg, errs := builder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
// Walk all leaf values from the root (depth -1 means unlimited).
ctx := context.Background()
valueCh, err := cfg.Walk(ctx, config.NewKeyPath(""), -1)
if err != nil {
fmt.Printf("Walk error: %v\n", err)
return
}
allKeys := make([]string, 0, 3)
for val := range valueCh {
allKeys = append(allKeys, val.Meta().Key.String())
}
sort.Strings(allKeys)
fmt.Printf("All keys: %v\n", allKeys)
// Walk from a sub-path to iterate only within "database".
valueCh, err = cfg.Walk(ctx, config.NewKeyPath("database"), -1)
if err != nil {
fmt.Printf("Walk error: %v\n", err)
return
}
subKeys := make([]string, 0, 3)
for val := range valueCh {
subKeys = append(subKeys, val.Meta().Key.String())
}
sort.Strings(subKeys)
fmt.Printf("Database keys: %v\n", subKeys)
// Walk with depth=2 limits traversal depth (stops before reaching pool/max_size).
valueCh, err = cfg.Walk(ctx, config.NewKeyPath("database"), 2)
if err != nil {
fmt.Printf("Walk error: %v\n", err)
return
}
shallowKeys := make([]string, 0, 2)
for val := range valueCh {
shallowKeys = append(shallowKeys, val.Meta().Key.String())
}
sort.Strings(shallowKeys)
fmt.Printf("Shallow keys (depth=2): %v\n", shallowKeys)
// Output:
// All keys: [database/host database/pool/max_size database/port]
// Database keys: [database/host database/pool/max_size database/port]
// Shallow keys (depth=2): [database/host database/port]
}
// Example_sliceConfig demonstrates Config.Slice() for extracting
// a sub-configuration as a separate Config object.
func Example_sliceConfig() {
data := map[string]any{
"server": map[string]any{
"http": map[string]any{
"port": 8080,
"host": "0.0.0.0",
},
"grpc": map[string]any{
"port": 9090,
},
},
}
builder := config.NewBuilder()
builder = builder.AddCollector(collectors.NewMap(data).WithName("config"))
cfg, errs := builder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
// Slice extracts "server/http" as a standalone Config.
httpCfg, err := cfg.Slice(config.NewKeyPath("server/http"))
if err != nil {
fmt.Printf("Slice error: %v\n", err)
return
}
// Access values relative to the sliced root.
var port int
_, err = httpCfg.Get(config.NewKeyPath("port"), &port)
if err != nil {
fmt.Printf("Get error: %v\n", err)
return
}
fmt.Printf("HTTP port: %d\n", port)
var host string
_, err = httpCfg.Get(config.NewKeyPath("host"), &host)
if err != nil {
fmt.Printf("Get error: %v\n", err)
return
}
fmt.Printf("HTTP host: %s\n", host)
// Slice returns an error for non-existent paths.
_, err = cfg.Slice(config.NewKeyPath("nonexistent"))
fmt.Printf("Nonexistent slice error: %v\n", err)
// Output:
// HTTP port: 8080
// HTTP host: 0.0.0.0
// Nonexistent slice error: path not found: nonexistent
}
// Example_effectiveAll demonstrates Config.EffectiveAll() which resolves
// effective configurations for ALL leaf entities in the hierarchy at once.
func Example_effectiveAll() {
data := map[string]any{
"replication": map[string]any{"failover": "manual"},
"groups": map[string]any{
"storages": map[string]any{
"sharding": map[string]any{"roles": []any{"storage"}},
"replicasets": map[string]any{
"s-001": map[string]any{
"leader": "s-001-a",
"instances": map[string]any{
"s-001-a": map[string]any{
"iproto": map[string]any{"listen": "127.0.0.1:3301"},
},
"s-001-b": map[string]any{
"iproto": map[string]any{"listen": "127.0.0.1:3302"},
},
},
},
},
},
},
}
builder := config.NewBuilder()
builder = builder.AddCollector(collectors.NewMap(data).WithName("config"))
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
// EffectiveAll resolves all leaf entities at once.
allConfigs, err := cfg.EffectiveAll()
if err != nil {
fmt.Printf("EffectiveAll error: %v\n", err)
return
}
// Sort keys for stable output.
keys := make([]string, 0, len(allConfigs))
for k := range allConfigs {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
instanceCfg := allConfigs[key]
var listen string
_, err := instanceCfg.Get(config.NewKeyPath("iproto/listen"), &listen)
if err != nil {
fmt.Printf("Get error: %v\n", err)
continue
}
var failover string
_, err = instanceCfg.Get(config.NewKeyPath("replication/failover"), &failover)
if err != nil {
fmt.Printf("Get error: %v\n", err)
continue
}
fmt.Printf("%s: listen=%s failover=%s\n", key, listen, failover)
}
// Output:
// groups/storages/replicasets/s-001/instances/s-001-a: listen=127.0.0.1:3301 failover=manual
// groups/storages/replicasets/s-001/instances/s-001-b: listen=127.0.0.1:3302 failover=manual
}
// Example_mutableConfig demonstrates MutableConfig with Set, Merge, and Update
// methods for modifying configuration at runtime.
func Example_mutableConfig() {
data := map[string]any{
"server": map[string]any{
"host": "localhost",
"port": 8080,
},
"debug": false,
}
builder := config.NewBuilder()
builder = builder.AddCollector(collectors.NewMap(data).WithName("config"))
cfg, errs := builder.BuildMutable(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
// Set overwrites a single value.
err := cfg.Set(config.NewKeyPath("server/port"), 9090)
if err != nil {
fmt.Printf("Set error: %v\n", err)
return
}
var port int
_, err = cfg.Get(config.NewKeyPath("server/port"), &port)
if err != nil {
fmt.Printf("Get error: %v\n", err)
return
}
fmt.Printf("Port after Set: %d\n", port)
// Merge adds all values from another Config (overrides existing keys).
overrideData := map[string]any{
"debug": true,
}
overrideBuilder := config.NewBuilder()
overrideBuilder = overrideBuilder.AddCollector(collectors.NewMap(overrideData))
overrideCfg, errs := overrideBuilder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
err = cfg.Merge(&overrideCfg)
if err != nil {
fmt.Printf("Merge error: %v\n", err)
return
}
var debug bool
_, err = cfg.Get(config.NewKeyPath("debug"), &debug)
if err != nil {
fmt.Printf("Get error: %v\n", err)
return
}
fmt.Printf("Debug after Merge: %v\n", debug)
// Update only modifies keys that already exist in the config.
updateData := map[string]any{
"server": map[string]any{
"host": "0.0.0.0",
},
"new_key": "ignored",
}
updateBuilder := config.NewBuilder()
updateBuilder = updateBuilder.AddCollector(collectors.NewMap(updateData))
updateCfg, errs := updateBuilder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
err = cfg.Update(&updateCfg)
if err != nil {
fmt.Printf("Update error: %v\n", err)
return
}
var host string
_, err = cfg.Get(config.NewKeyPath("server/host"), &host)
if err != nil {
fmt.Printf("Get error: %v\n", err)
return
}
fmt.Printf("Host after Update: %s\n", host)
// Verify that new_key was not added by Update.
_, ok := cfg.Lookup(config.NewKeyPath("new_key"))
fmt.Printf("new_key exists: %v\n", ok)
// Output:
// Port after Set: 9090
// Debug after Merge: true
// Host after Update: 0.0.0.0
// new_key exists: false
}