-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_api_test.go
More file actions
215 lines (193 loc) · 5.33 KB
/
Copy pathexample_api_test.go
File metadata and controls
215 lines (193 loc) · 5.33 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
package fastconf_test
import (
"context"
"fmt"
"os"
"path/filepath"
"testing/fstest"
"github.com/fastabc/fastconf"
)
type apiExampleConfig struct {
Server struct {
Addr string `json:"addr" yaml:"addr"`
} `json:"server" yaml:"server"`
}
// ExampleNew demonstrates the shortest typed entry path: construct a manager,
// read the live value, and close it when the owner shuts down.
func ExampleNew() {
mgr, err := fastconf.New[apiExampleConfig](context.Background(),
fastconf.PresetTesting(fastconf.TestingOpts{
FS: fstest.MapFS{
"conf.d/base/00-app.yaml": &fstest.MapFile{
Data: []byte("server:\n addr: \":8080\"\n"),
},
},
}),
)
if err != nil {
fmt.Println(err)
return
}
defer mgr.Close()
fmt.Println(mgr.Get().Server.Addr)
// Output:
// :8080
}
// ExampleSubscribe demonstrates reacting to a typed subtree after a successful
// commit. Subscribe fires only when the extracted value actually changes;
// callers no longer need an inline equality check.
func ExampleSubscribe() {
mgr, err := fastconf.New[apiExampleConfig](context.Background(),
fastconf.PresetTesting(fastconf.TestingOpts{
FS: fstest.MapFS{
"conf.d/base/00-app.yaml": &fstest.MapFile{
Data: []byte("server:\n addr: \":8080\"\n"),
},
},
}),
)
if err != nil {
fmt.Println(err)
return
}
defer mgr.Close()
cancel := fastconf.Subscribe(mgr,
func(c *apiExampleConfig) *string { return &c.Server.Addr },
func(old, next *string) {
fmt.Printf("%s -> %s\n", *old, *next)
},
)
defer cancel()
_ = mgr.Reload(context.Background(), fastconf.WithSourceOverride(map[string]any{
"server": map[string]any{"addr": ":9090"},
}))
// Output:
// :8080 -> :9090
}
// ExampleManager_Errors demonstrates the asynchronous failure stream that lets
// services centralize reload error handling without blocking the writer.
func ExampleManager_Errors() {
mgr, err := fastconf.New[apiExampleConfig](context.Background(),
fastconf.PresetTesting(fastconf.TestingOpts{
FS: fstest.MapFS{
"conf.d/base/00-app.yaml": &fstest.MapFile{
Data: []byte("server:\n addr: \":8080\"\n"),
},
},
}),
fastconf.WithValidator(func(c *apiExampleConfig) error {
if c.Server.Addr == "" {
return fmt.Errorf("server.addr is required")
}
return nil
}),
)
if err != nil {
fmt.Println(err)
return
}
defer mgr.Close()
_ = mgr.Reload(context.Background(), fastconf.WithSourceOverride(map[string]any{
"server": map[string]any{"addr": ""},
}))
re := <-mgr.Errors()
fmt.Println(re.Reason, re.Err != nil)
// Output:
// override true
}
// ExampleManager_Plan demonstrates previewing a file-backed change before it
// becomes the live snapshot.
func ExampleManager_Plan() {
root := mustExampleTempDir("example-plan-")
defer os.RemoveAll(root)
confDir := filepath.Join(root, "conf.d")
configPath := filepath.Join(confDir, "base", "00-app.yaml")
mustWriteExampleFile(configPath, "server:\n addr: \":8080\"\n")
mgr, err := fastconf.New[apiExampleConfig](context.Background(),
fastconf.WithDir(confDir),
)
if err != nil {
fmt.Println(err)
return
}
defer mgr.Close()
mustWriteExampleFile(configPath, "server:\n addr: \":9090\"\n")
plan, err := mgr.Plan().Run(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(len(plan.Diff), plan.Proposed.Value().Server.Addr, mgr.Get().Server.Addr)
// Output:
// 1 :9090 :8080
}
// ExampleReplay_Rollback demonstrates recovering a retained prior snapshot
// without rerunning the reload pipeline.
func ExampleReplay_Rollback() {
root := mustExampleTempDir("example-replay-")
defer os.RemoveAll(root)
confDir := filepath.Join(root, "conf.d")
configPath := filepath.Join(confDir, "base", "00-app.yaml")
mustWriteExampleFile(configPath, "server:\n addr: \":8080\"\n")
mgr, err := fastconf.New[apiExampleConfig](context.Background(),
fastconf.WithDir(confDir),
fastconf.WithHistory(2),
)
if err != nil {
fmt.Println(err)
return
}
defer mgr.Close()
mustWriteExampleFile(configPath, "server:\n addr: \":9090\"\n")
if err := mgr.Reload(context.Background()); err != nil {
fmt.Println(err)
return
}
liveAfterReload := mgr.Get().Server.Addr
history := mgr.Replay().List()
if err := mgr.Replay().Rollback(history[0]); err != nil {
fmt.Println(err)
return
}
fmt.Println(liveAfterReload, mgr.Get().Server.Addr)
// Output:
// :9090 :8080
}
// ExampleMustNew demonstrates the one-line top-level initialisation
// pattern. MustNew panics when the initial reload fails, so it is
// intended for main / init in command-line tools and tests — not for
// long-running daemons that should degrade gracefully.
func ExampleMustNew() {
mgr := fastconf.MustNew[apiExampleConfig](context.Background(),
fastconf.PresetTesting(fastconf.TestingOpts{
FS: fstest.MapFS{
"conf.d/base/00-app.yaml": &fstest.MapFile{
Data: []byte("server:\n addr: \":8080\"\n"),
},
},
}),
)
defer mgr.Close()
fmt.Println(mgr.Get().Server.Addr)
// Output:
// :8080
}
func mustExampleTempDir(pattern string) string {
dir, err := os.MkdirTemp(".", pattern)
if err != nil {
panic(err)
}
abs, err := filepath.Abs(dir)
if err != nil {
panic(err)
}
return abs
}
func mustWriteExampleFile(path, content string) {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
panic(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
panic(err)
}
}