forked from rbaliyan/config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
102 lines (82 loc) · 2 KB
/
example_test.go
File metadata and controls
102 lines (82 loc) · 2 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
package config_test
import (
"context"
"fmt"
"github.com/rbaliyan/config"
"github.com/rbaliyan/config/memory"
)
func ExampleNew() {
ctx := context.Background()
// Create a manager with an in-memory store
mgr, err := config.New(config.WithStore(memory.NewStore()))
if err != nil {
fmt.Println("Error:", err)
return
}
// Connect to the backend
if err := mgr.Connect(ctx); err != nil {
fmt.Println("Error:", err)
return
}
defer mgr.Close(ctx)
// Get a namespaced config and set a value
cfg := mgr.Namespace("production")
if err := cfg.Set(ctx, "app/timeout", 30); err != nil {
fmt.Println("Error:", err)
return
}
// Read the value back
val, err := cfg.Get(ctx, "app/timeout")
if err != nil {
fmt.Println("Error:", err)
return
}
i, _ := val.Int64()
fmt.Println("timeout:", i)
// Output: timeout: 30
}
func ExampleConfig_Get() {
ctx := context.Background()
mgr, _ := config.New(config.WithStore(memory.NewStore()))
_ = mgr.Connect(ctx)
defer mgr.Close(ctx)
cfg := mgr.Namespace("app")
_ = cfg.Set(ctx, "feature/dark-mode", true)
val, err := cfg.Get(ctx, "feature/dark-mode")
if err != nil {
fmt.Println("Error:", err)
return
}
enabled, _ := val.Bool()
fmt.Println("dark mode:", enabled)
// Output: dark mode: true
}
func ExampleNewValue() {
// Create a value and inspect its type
val := config.NewValue(42)
fmt.Println("type:", val.Type())
i, err := val.Int64()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("value:", i)
// Output:
// type: int
// value: 42
}
func ExampleContextWithManager() {
ctx := context.Background()
mgr, _ := config.New(config.WithStore(memory.NewStore()))
_ = mgr.Connect(ctx)
defer mgr.Close(ctx)
// Add manager and namespace to context
ctx = config.ContextWithManager(ctx, mgr)
ctx = config.ContextWithNamespace(ctx, "myapp")
// Set and get via context convenience functions
_ = config.Set(ctx, "greeting", "hello")
val, _ := config.Get(ctx, "greeting")
s, _ := val.String()
fmt.Println(s)
// Output: hello
}