-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
91 lines (80 loc) · 2.65 KB
/
Copy pathexample_test.go
File metadata and controls
91 lines (80 loc) · 2.65 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
package external_source_test
import (
"context"
"fmt"
"testing/fstest"
"github.com/fastabc/fastconf"
"github.com/fastabc/fastconf/codec"
"github.com/fastabc/fastconf/contracts"
"github.com/fastabc/fastconf/providers/source"
)
type externalSourceExampleConfig struct {
Server struct {
Addr string `yaml:"addr" json:"addr"`
} `yaml:"server" json:"server"`
Feature struct {
BetaEnabled bool `yaml:"betaEnabled" json:"betaEnabled"`
} `yaml:"feature" json:"feature"`
}
// staticExampleProvider implements contracts.Provider directly because
// its data is already structured (no bytes → decode required). The Source
// contract is the alternative used by byte-blob layers (see seed below).
type staticExampleProvider struct {
name string
priority int
data map[string]any
}
func (p *staticExampleProvider) Name() string { return p.name }
func (p *staticExampleProvider) Priority() int { return p.priority }
func (p *staticExampleProvider) Load(context.Context) (map[string]any, error) {
out := make(map[string]any, len(p.data))
for k, v := range p.data {
out[k] = v
}
return out, nil
}
func (p *staticExampleProvider) Watch(context.Context) (<-chan contracts.Event, error) {
return nil, nil
}
// Example_externalSource demonstrates the two complementary extension
// points for fastconf:
//
// - WithSource(Source, Parser) for byte-blob layers (file / http /
// inline bytes) where the decoder is named at the call site;
// - WithProvider for already-structured contributors (env / cli /
// KV with one key per setting).
//
// See also: docs/cookbook/cross-process.md — wiring real NATS / Redis
// Streams providers via the same WithProvider entry point.
func Example_externalSource() {
demo := &staticExampleProvider{
name: "demo-static",
priority: contracts.PriorityKV,
data: map[string]any{
"server": map[string]any{"addr": ":9090"},
"feature": map[string]any{"betaEnabled": true},
},
}
// Inline byte-blob layer paired with an explicit Parser. To let
// `demo` (PriorityKV=30) supply the override, push the seed below
// it via WithPriority.
seed := source.NewBytes("seed", "yaml",
[]byte("server:\n addr: \":8080\"\nfeature:\n betaEnabled: false\n"),
).WithPriority(contracts.PriorityStatic)
mgr, err := fastconf.New[externalSourceExampleConfig](context.Background(),
fastconf.WithFS(fstest.MapFS{
"conf.d/base/.keep": &fstest.MapFile{Data: []byte("")},
}),
fastconf.WithSource(seed, codec.YAMLParser()),
fastconf.WithProvider(demo),
)
if err != nil {
fmt.Println(err)
return
}
defer mgr.Close()
app := mgr.Get()
fmt.Printf("%s %t\n", app.Server.Addr, app.Feature.BetaEnabled)
// Output:
// :9090 true
}