forked from samber/do
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injector_example_test.go
143 lines (110 loc) · 2.74 KB
/
injector_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
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
package do
import (
"database/sql"
"fmt"
)
type dbService struct {
db *sql.DB
}
func (s *dbService) HealthCheck() error {
return nil
}
func (s *dbService) Shutdown() error {
return nil
}
func dbServiceProvider(i *Injector) (*dbService, error) {
return &dbService{db: nil}, nil
}
func ExampleNew() {
injector := New()
ProvideNamedValue(injector, "PG_URI", "postgres://user:pass@host:5432/db")
uri, err := InvokeNamed[string](injector, "PG_URI")
fmt.Println(uri)
fmt.Println(err)
// Output:
// postgres://user:pass@host:5432/db
// <nil>
}
func ExampleDefaultInjector() {
ProvideNamedValue(nil, "PG_URI", "postgres://user:pass@host:5432/db")
uri, err := InvokeNamed[string](nil, "PG_URI")
fmt.Println(uri)
fmt.Println(err)
// Output:
// postgres://user:pass@host:5432/db
// <nil>
}
func ExampleNewWithOpts() {
injector := NewWithOpts(&InjectorOpts{
HookAfterShutdown: func(injector *Injector, serviceName string) {
fmt.Printf("service shutdown: %s\n", serviceName)
},
})
ProvideNamed(injector, "PG_URI", func(i *Injector) (string, error) {
return "postgres://user:pass@host:5432/db", nil
})
MustInvokeNamed[string](injector, "PG_URI")
_ = injector.Shutdown()
// Output:
// service shutdown: PG_URI
}
func ExampleInjector_ListProvidedServices() {
injector := New()
ProvideNamedValue(injector, "PG_URI", "postgres://user:pass@host:5432/db")
services := injector.ListProvidedServices()
fmt.Println(services)
// Output:
// [PG_URI]
}
func ExampleInjector_ListInvokedServices_invoked() {
injector := New()
type test struct {
foobar string
}
ProvideNamed(injector, "SERVICE_NAME", func(i *Injector) (test, error) {
return test{foobar: "foobar"}, nil
})
_, _ = InvokeNamed[test](injector, "SERVICE_NAME")
services := injector.ListInvokedServices()
fmt.Println(services)
// Output:
// [SERVICE_NAME]
}
func ExampleInjector_ListInvokedServices_notInvoked() {
injector := New()
type test struct {
foobar string
}
ProvideNamed(injector, "SERVICE_NAME", func(i *Injector) (test, error) {
return test{foobar: "foobar"}, nil
})
services := injector.ListInvokedServices()
fmt.Println(services)
// Output:
// []
}
func ExampleInjector_HealthCheck() {
injector := New()
Provide(injector, dbServiceProvider)
health := injector.HealthCheck()
fmt.Println(health)
// Output:
// map[*do.dbService:<nil>]
}
func ExampleInjector_Shutdown() {
injector := New()
Provide(injector, dbServiceProvider)
err := injector.Shutdown()
fmt.Println(err)
// Output:
// <nil>
}
func ExampleInjector_Clone() {
injector := New()
ProvideNamedValue(injector, "PG_URI", "postgres://user:pass@host:5432/db")
injector2 := injector.Clone()
services := injector2.ListProvidedServices()
fmt.Println(services)
// Output:
// [PG_URI]
}