-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_example_test.go
More file actions
251 lines (223 loc) · 6.99 KB
/
Copy pathconfig_example_test.go
File metadata and controls
251 lines (223 loc) · 6.99 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
package core_test
import (
. "dappco.re/go"
)
// ExampleConfig_Set sets a value through `Config.Set` for service configuration. Callers
// write untyped settings and read back typed values or enabled features.
func ExampleConfig_Set() {
c := New()
c.Config().Set("database.host", "localhost")
c.Config().Set("database.port", 5432)
Println(c.Config().String("database.host"))
Println(c.Config().Int("database.port"))
// Output:
// localhost
// 5432
}
// ExampleNewConfigVar_config initialises configuration values through `NewConfigVar` for
// service configuration. Callers write untyped settings and read back typed values or
// enabled features.
func ExampleNewConfigVar_config() {
v := NewConfigVar("enabled")
Println(v.Get())
Println(v.IsSet())
// Output:
// enabled
// true
}
// ExampleConfigVar_Set sets a value through `ConfigVar.Set` for service configuration.
// Callers write untyped settings and read back typed values or enabled features.
func ExampleConfigVar_Set() {
var v ConfigVar[string]
v.Set("enabled")
Println(v.Get())
Println(v.IsSet())
// Output:
// enabled
// true
}
// ExampleConfigVar_Unset clears a value through `ConfigVar.Unset` for service
// configuration. Callers write untyped settings and read back typed values or enabled
// features.
func ExampleConfigVar_Unset() {
v := NewConfigVar("enabled")
v.Unset()
Println(v.Get())
Println(v.IsSet())
// Output:
//
// false
}
// ExampleConfig_New creates an empty configuration with no enabled feature flags. Callers
// write untyped settings and read back typed values or enabled features.
func ExampleConfig_New() {
cfg := (&Config{}).New()
Println(cfg.EnabledFeatures())
// Output: []
}
// ExampleConfigOptions groups settings and feature flags through `ConfigOptions` for
// service configuration. Callers write untyped settings and read back typed values or
// enabled features.
func ExampleConfigOptions() {
opts := ConfigOptions{
Settings: map[string]any{"host": "localhost"},
Features: map[string]bool{"debug": true},
}
Println(opts.Settings["host"])
Println(opts.Features["debug"])
// Output:
// localhost
// true
}
// ExampleConfig_Get retrieves a value through `Config.Get` for service configuration.
// Callers write untyped settings and read back typed values or enabled features.
func ExampleConfig_Get() {
cfg := (&Config{}).New()
cfg.Set("host", "localhost")
Println(cfg.Get("host").Value)
Println(cfg.Get("missing").OK)
// Output:
// localhost
// false
}
// ExampleConfig_String renders `Config.String` as a stable string for service
// configuration. Callers write untyped settings and read back typed values or enabled
// features.
func ExampleConfig_String() {
cfg := (&Config{}).New()
cfg.Set("host", "localhost")
Println(cfg.String("host"))
// Output: localhost
}
// ExampleConfig_Int reads an integer through `Config.Int` for service configuration.
// Callers write untyped settings and read back typed values or enabled features.
func ExampleConfig_Int() {
cfg := (&Config{}).New()
cfg.Set("port", 8080)
Println(cfg.Int("port"))
// Output: 8080
}
// ExampleConfig_Bool reads a boolean through `Config.Bool` for service configuration.
// Callers write untyped settings and read back typed values or enabled features.
func ExampleConfig_Bool() {
cfg := (&Config{}).New()
cfg.Set("debug", true)
Println(cfg.Bool("debug"))
// Output: true
}
// ExampleConfigGet reads a typed config value through `ConfigGet` for service
// configuration. Callers write untyped settings and read back typed values or enabled
// features.
func ExampleConfigGet() {
cfg := (&Config{}).New()
cfg.Set("host", "localhost")
Println(ConfigGet[string](cfg, "host"))
// Output: localhost
}
// ExampleConfig_Enable turns on named feature flags for service configuration. Callers
// write untyped settings and read back typed values or enabled features.
func ExampleConfig_Enable() {
c := New()
c.Config().Enable("dark-mode")
c.Config().Enable("beta-features")
Println(c.Config().Enabled("dark-mode"))
features := c.Config().EnabledFeatures()
SliceSort(features)
Println(features)
// Output:
// true
// [beta-features dark-mode]
}
// ExampleConfig_Disable turns off a previously enabled feature flag for service
// configuration. Callers write untyped settings and read back typed values or enabled
// features.
func ExampleConfig_Disable() {
c := New()
c.Config().Enable("debug")
c.Config().Disable("debug")
Println(c.Config().Enabled("debug"))
// Output: false
}
// ExampleConfig_Enabled checks whether a feature is enabled through `Config.Enabled` for
// service configuration. Callers write untyped settings and read back typed values or
// enabled features.
func ExampleConfig_Enabled() {
c := New()
c.Config().Enable("debug")
Println(c.Config().Enabled("debug"))
// Output: true
}
// ExampleConfig_EnabledFeatures lists enabled feature flags through
// `Config.EnabledFeatures` for service configuration. Callers write untyped settings and
// read back typed values or enabled features.
func ExampleConfig_EnabledFeatures() {
c := New()
c.Config().Enable("beta")
c.Config().Enable("debug")
features := c.Config().EnabledFeatures()
SliceSort(features)
Println(features)
// Output: [beta debug]
}
// ExampleConfigVar toggles a standalone config variable through `ConfigVar` for service
// configuration. Callers write untyped settings and read back typed values or enabled
// features.
func ExampleConfigVar() {
v := NewConfigVar(42)
Println(v.Get(), v.IsSet())
v.Unset()
Println(v.Get(), v.IsSet())
// Output:
// 42 true
// 0 false
}
// ExampleConfig_Group scopes config keys under a group prefix through
// `Config.Group` — the view reads and writes "<group>.<key>" in the shared store.
func ExampleConfig_Group() {
c := New()
db := c.Config("database")
db.Set("host", "localhost")
Println(c.Config().String("database.host"))
Println(db.String("host"))
// Output:
// localhost
// localhost
}
// ExampleConfigVar_Get reads a typed config var through `ConfigVar.Get`.
func ExampleConfigVar_Get() {
v := NewConfigVar("https://api.lthn.ai")
Println(v.Get())
// Output: https://api.lthn.ai
}
// ExampleConfigVar_IsSet reports whether a config var holds a value through `ConfigVar.IsSet`.
func ExampleConfigVar_IsSet() {
v := NewConfigVar("ready")
Println(v.IsSet())
// Output: true
}
// ExampleFeature_Name returns the feature's name through `Feature.Name`.
func ExampleFeature_Name() {
Println(New().Feature("dark-mode").Name())
// Output: dark-mode
}
// ExampleFeature_Enable activates a feature through `Feature.Enable`.
func ExampleFeature_Enable() {
c := New()
c.Feature("beta").Enable()
Println(c.Feature("beta").Enabled())
// Output: true
}
// ExampleFeature_Disable deactivates a feature through `Feature.Disable`.
func ExampleFeature_Disable() {
c := New()
f := c.Feature("beta")
f.Enable()
f.Disable()
Println(f.Enabled())
// Output: false
}
// ExampleFeature_Enabled reports whether a feature is active through `Feature.Enabled`.
func ExampleFeature_Enabled() {
Println(New().Feature("unset").Enabled())
// Output: false
}