-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_storage_test.go
More file actions
221 lines (168 loc) · 5.57 KB
/
example_storage_test.go
File metadata and controls
221 lines (168 loc) · 5.57 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
package config_test
import (
"context"
"fmt"
"io"
"github.com/tarantool/go-config"
"github.com/tarantool/go-config/collectors"
"github.com/tarantool/go-config/internal/testutil"
)
// Example_storageCollector demonstrates reading multiple configuration
// documents from a key-value storage under a common prefix using the
// Storage collector.
func Example_storageCollector() {
// Set up in-memory storage with configuration data.
mock := testutil.NewMockStorage()
testutil.PutIntegrity(mock, "/config/", "app", []byte("port: 8080\nhost: localhost"))
// Create an integrity-typed wrapper and the Storage collector.
typed := testutil.NewRawTyped(mock, "/config/")
collector := collectors.NewStorage(typed, "/config/", collectors.NewYamlFormat())
// Build configuration.
builder := config.NewBuilder()
builder = builder.AddCollector(collector)
cfg, errs := builder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
var port int
_, err := cfg.Get(config.NewKeyPath("port"), &port)
if err != nil {
fmt.Printf("Get port error: %v\n", err)
return
}
var host string
_, err = cfg.Get(config.NewKeyPath("host"), &host)
if err != nil {
fmt.Printf("Get host error: %v\n", err)
return
}
fmt.Printf("Host: %s\n", host)
fmt.Printf("Port: %d\n", port)
// Output:
// Host: localhost
// Port: 8080
}
// Example_storageCollectorMultipleKeys demonstrates reading and merging
// multiple keys from storage into a unified configuration tree. Key names
// are used only for distinguishing documents; the YAML content determines
// the tree structure.
func Example_storageCollectorMultipleKeys() {
mock := testutil.NewMockStorage()
testutil.PutIntegrity(mock, "/config/", "cfg-servers",
[]byte("server:\n port: 8080\n host: localhost"))
testutil.PutIntegrity(mock, "/config/", "cfg-database",
[]byte("database:\n driver: postgres\n port: 5432"))
typed := testutil.NewRawTyped(mock, "/config/")
collector := collectors.NewStorage(typed, "/config/", collectors.NewYamlFormat())
builder := config.NewBuilder()
builder = builder.AddCollector(collector)
cfg, errs := builder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
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("server host: %s\n", host)
var driver string
_, err = cfg.Get(config.NewKeyPath("database/driver"), &driver)
if err != nil {
fmt.Printf("Get error: %v\n", err)
return
}
fmt.Printf("database driver: %s\n", driver)
// Output:
// server host: localhost
// database driver: postgres
}
// Example_storageCollectorWithMapOverride demonstrates combining a Storage
// collector with a Map collector, where later collectors override earlier ones.
func Example_storageCollectorWithMapOverride() {
mock := testutil.NewMockStorage()
testutil.PutIntegrity(mock, "/config/", "db",
[]byte("db:\n host: storage-host\n port: 5432"))
typed := testutil.NewRawTyped(mock, "/config/")
storageCollector := collectors.NewStorage(typed, "/config/", collectors.NewYamlFormat())
// Map collector provides defaults; storage collector overrides the host.
mapCollector := collectors.NewMap(map[string]any{
"db/host": "override-host",
})
builder := config.NewBuilder()
builder = builder.AddCollector(mapCollector)
builder = builder.AddCollector(storageCollector)
cfg, errs := builder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
return
}
var host string
_, err := cfg.Get(config.NewKeyPath("db/host"), &host)
if err != nil {
fmt.Printf("Get error: %v\n", err)
return
}
fmt.Printf("Host: %s\n", host)
// Output:
// Host: storage-host
}
// Example_storageSource demonstrates using StorageSource as a DataSource
// to read a single configuration document from storage with integrity
// verification.
func Example_storageSource() {
mock := testutil.NewMockStorage()
testutil.PutIntegrity(mock, "/config/", "app",
[]byte("server:\n port: 8080\n host: localhost"))
// Create a StorageSource for a single key.
source := collectors.NewStorageSource(mock, "/config/", "app", nil, nil)
// Use it with a format to build a collector.
collector, err := collectors.NewSource(context.Background(), source, collectors.NewYamlFormat())
if err != nil {
fmt.Printf("NewSource error: %v\n", err)
return
}
builder := config.NewBuilder()
builder = builder.AddCollector(collector)
cfg, errs := builder.Build(context.Background())
if len(errs) > 0 {
fmt.Printf("Build errors: %v\n", errs)
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("Server port: %d\n", port)
// Output:
// Server port: 8080
}
// Example_storageSourceFetchStream demonstrates using StorageSource.FetchStream
// to read raw configuration bytes from storage.
func Example_storageSourceFetchStream() {
mock := testutil.NewMockStorage()
testutil.PutIntegrity(mock, "/config/", "app", []byte("key: value"))
source := collectors.NewStorageSource(mock, "/config/", "app", nil, nil)
ctx := context.Background()
reader, err := source.FetchStream(ctx)
if err != nil {
fmt.Printf("FetchStream error: %v\n", err)
return
}
defer func() { _ = reader.Close() }()
data, err := io.ReadAll(reader)
if err != nil {
fmt.Printf("ReadAll error: %v\n", err)
return
}
fmt.Printf("Data: %s\n", string(data))
fmt.Printf("Revision: %s\n", source.Revision())
// Output:
// Data: key: value
// Revision: 1
}