-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
225 lines (177 loc) · 4.79 KB
/
examples_test.go
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
222
223
224
225
package module_test
import (
"context"
"fmt"
"regexp"
"github.com/googollee/module"
)
type DB interface {
Target() string
}
type db struct {
target string
}
func (db *db) Target() string {
return db.target
}
var (
ModuleDB = module.New[DB]()
)
type Cache struct {
fallback DB
keyPrefix string
}
var (
ModuleCache = module.New[*Cache]()
ProvideCache = ModuleCache.ProvideWithFunc(func(ctx context.Context) (*Cache, error) {
db := ModuleDB.Value(ctx)
return &Cache{
fallback: db,
keyPrefix: "cache",
}, nil
})
)
func ExampleModule() {
repo := module.NewRepo()
// No order required when adding providers
repo.Add(ProvideCache)
repo.Add(ModuleDB.ProvideValue(&db{target: "local.db"}))
ctx := context.Background()
ctx, err := repo.InjectTo(ctx)
if err != nil {
fmt.Println("inject error:", err)
return
}
db := ModuleDB.Value(ctx)
cache := ModuleCache.Value(ctx)
fmt.Println("db target:", db.Target())
fmt.Println("cache fallback target:", cache.fallback.Target())
// Output:
// db target: local.db
// cache fallback target: local.db
}
func ExampleModule_loadOtherValue() {
type Key string
targetKey := Key("target")
repo := module.NewRepo()
repo.Add(ModuleDB.ProvideWithFunc(func(ctx context.Context) (DB, error) {
// Load the target value from the context.
target := ctx.Value(targetKey).(string)
return &db{
target: target,
}, nil
}))
repo.Add(ProvideCache)
// Store the target value in the context.
ctx := context.WithValue(context.Background(), targetKey, "target.db")
ctx, err := repo.InjectTo(ctx)
if err != nil {
fmt.Println("inject error:", err)
return
}
db := ModuleDB.Value(ctx)
cache := ModuleCache.Value(ctx)
fmt.Println("db target:", db.Target())
fmt.Println("cache fallback target:", cache.fallback.Target())
// Output:
// db target: target.db
// cache fallback target: target.db
}
func ExampleModule_newPrefixInSpan() {
repo := module.NewRepo()
repo.Add(ModuleDB.ProvideValue(&db{target: "local.db"}))
repo.Add(ProvideCache)
ctx := context.Background()
ctx, err := repo.InjectTo(ctx)
if err != nil {
fmt.Println("inject error:", err)
return
}
db := ModuleDB.Value(ctx)
cache := ModuleCache.Value(ctx)
fmt.Println("before span, db target:", db.Target())
fmt.Println("before span, cache prefix:", cache.keyPrefix)
{
// a new context in the span
ctx := ModuleCache.With(ctx, &Cache{
fallback: db,
keyPrefix: "span",
})
db := ModuleDB.Value(ctx)
cache := ModuleCache.Value(ctx)
fmt.Println("in span, db target:", db.Target())
fmt.Println("in span, cache prefix:", cache.keyPrefix)
}
db = ModuleDB.Value(ctx)
cache = ModuleCache.Value(ctx)
fmt.Println("after span, db target:", db.Target())
fmt.Println("after span, cache fallback target:", cache.keyPrefix)
// Output:
// before span, db target: local.db
// before span, cache prefix: cache
// in span, db target: local.db
// in span, cache prefix: span
// after span, db target: local.db
// after span, cache fallback target: cache
}
func ExampleModule_createWithError() {
repo := module.NewRepo()
repo.Add(ModuleDB.ProvideValue(&db{target: "local.db"}))
repo.Add(ModuleCache.ProvideWithFunc(func(ctx context.Context) (*Cache, error) {
_ = ModuleDB.Value(ctx)
return nil, fmt.Errorf("new cache error")
}))
ctx := context.Background()
_, err := repo.InjectTo(ctx)
if err != nil {
fmt.Println("inject error:", err)
return
}
// Output:
// inject error: creating with module *module_test.Cache: new cache error
}
func ExampleModule_createWithPanic() {
repo := module.NewRepo()
repo.Add(ModuleDB.ProvideValue(&db{target: "localhost.db"}))
repo.Add(ModuleCache.ProvideWithFunc(func(ctx context.Context) (*Cache, error) {
_ = ModuleDB.Value(ctx)
panic(fmt.Errorf("new cache error"))
}))
defer func() {
err := recover()
fmt.Println("panic:", err)
}()
ctx := context.Background()
_, err := repo.InjectTo(ctx)
if err != nil {
fmt.Println("inject error:", err)
return
}
// Output:
// panic: new cache error
}
func ExampleModule_notExistingProvider() {
ctx := context.Background()
repo := module.NewRepo()
repo.Add(ProvideCache)
// repo.Add(ModuleDB.ProvideValue())
_, err := repo.InjectTo(ctx)
if err != nil {
fmt.Println("inject error:", err)
return
}
// Output:
// inject error: creating with module module_test.DB: can't find module
}
func ExampleModule_duplicatingProviders() {
defer func() {
p := recover().(string)
// Remove the file line info for testing.
fmt.Println("panic:", regexp.MustCompile(`at .*`).ReplaceAllString(p, "at <removed file and line>"))
}()
repo := module.NewRepo()
repo.Add(ModuleDB.ProvideValue(&db{target: "real.db"}))
repo.Add(ModuleDB.ProvideValue(&db{target: "fake.db"}))
// Output:
// panic: already have a provider with type "module_test.DB", added at <removed file and line>
}