-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample_test.go
72 lines (63 loc) · 1.63 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
package di_test
import (
"context"
"fmt"
"strings"
"github.com/go-tk/di"
)
func Example() {
var program di.Program
showUserNameList(&program)
modifyUserNameList(&program)
provideUserNameList(&program)
provideAdditionalUserName(&program)
// NOTE: Program will rearrange Functions properly basing on dependency analysis.
defer program.Clean()
program.MustRun(context.Background())
// Output:
// user name list: tom,jeff,spike
}
func provideUserNameList(program *di.Program) {
var userNameList []string
program.MustNewFunction(
di.Result("USER_NAME_LIST", &userNameList),
di.Body(func(context.Context) error {
userNameList = []string{"tom", "jeff"}
return nil
}),
)
}
func provideAdditionalUserName(program *di.Program) {
var additionalUserName string
program.MustNewFunction(
di.Result("ADDITIONAL_USER_NAME", &additionalUserName),
di.Body(func(context.Context) error {
additionalUserName = "spike"
return nil
}),
)
}
func showUserNameList(program *di.Program) {
var userNameList []string
program.MustNewFunction(
di.Argument("USER_NAME_LIST", &userNameList),
di.Body(func(context.Context) error {
fmt.Printf("user name list: %v\n", strings.Join(userNameList, ","))
return nil
}),
)
}
func modifyUserNameList(program *di.Program) {
var (
additionalUserName string
userNameList *[]string
)
program.MustNewFunction(
di.Argument("ADDITIONAL_USER_NAME", &additionalUserName),
di.Body(func(context.Context) error { return nil }),
di.Hook("USER_NAME_LIST", &userNameList, func(context.Context) error {
*userNameList = append(*userNameList, additionalUserName)
return nil
}),
)
}