-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
75 lines (58 loc) · 1.6 KB
/
example_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
package modules
import (
"fmt"
)
// The KVClient interface models a simple key/value store.
type KVClient interface {
Get(key string) string
Put(key, value string)
}
// A MapDBClient is a simple mock KVClient implementation backed by a map and configured with a default value for missing keys.
type MapDBClient struct {
defaultValue string
db map[string]string
}
func (client *MapDBClient) Get(key string) string {
if value, ok := client.db[key]; ok {
return value
} else {
return client.defaultValue
}
}
func (client *MapDBClient) Put(key, value string) {
client.db[key] = value
}
// A service module has a 'GetData' service which utilizes an injected DBClient.
type ServiceModule struct {
KVClient KVClient `inject:""`
}
func (service *ServiceModule) GetData(key string) string {
return service.KVClient.Get(key)
}
func (service *ServiceModule) StoreData(key, value string) {
service.KVClient.Put(key, value)
}
type defaultValue string
// This data module provides a KVClient.
type DataModule struct {
DefaultValue defaultValue
KVClient KVClient `provide:""`
}
func (data *DataModule) Provide() error {
data.KVClient = &MapDBClient{defaultValue: string(data.DefaultValue), db: make(map[string]string)}
return nil
}
func Example() {
serviceModule := &ServiceModule{}
dataModule := &DataModule{DefaultValue: "default"}
binder := NewBinder()
if err := binder.Bind(serviceModule, dataModule); err != nil {
panic(err)
}
fmt.Println(serviceModule.GetData("key"))
serviceModule.StoreData("key", "value")
fmt.Println(serviceModule.GetData("key"))
// Output:
// default
// value
}