-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_example_test.go
More file actions
193 lines (173 loc) · 5.49 KB
/
config_example_test.go
File metadata and controls
193 lines (173 loc) · 5.49 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
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
}