-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
192 lines (155 loc) · 4.18 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
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
// Copyright 2022 Sergey Novichkov. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
package di_test
import (
"errors"
"fmt"
"net/http"
"os"
"github.com/gozix/di"
)
// This is example demonstrates Container.Resolve usage with aliases.
func Example_interfaces() {
var builder, err = di.NewBuilder(
// provide constructor
di.Provide(NewBarController, di.As(new(Controller))),
di.Provide(NewBazController, di.As(new(Controller))),
)
if err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
var container di.Container
if container, err = builder.Build(); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
var controllers []Controller
if err = container.Resolve(&controllers); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
fmt.Println("resolved", len(controllers), "controllers")
// Output:
// resolved 2 controllers
}
// This is example demonstrates Container.Has usage with modifiers.
func Example_tags() {
var builder, err = di.NewBuilder(
// provide constructor
di.Provide(NewBarController, di.Tags{{
Name: "controller",
}}),
)
if err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
var container di.Container
if container, err = builder.Build(); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
if container.Has((*BarController)(nil), di.WithTags("controller")) {
fmt.Println("container has *BarController with tag controller")
}
var c *BarController
if err = container.Resolve(&c, di.WithTags("controller")); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
if c != nil {
fmt.Println("container resolved *BarController with tag controller")
}
err = container.Call(func(c *BarController) error {
if c == nil {
return errors.New("container call has nil *BarController")
}
fmt.Println("container call *BarController with tag controller")
return nil
}, di.Constraint(0, di.WithTags("controller")))
if err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
// Output:
// container has *BarController with tag controller
// container resolved *BarController with tag controller
// container call *BarController with tag controller
}
// This is example demonstrates optional field injection.
func Example_optionalFieldInjection() {
type Foo struct {
Bar *BarController
Baz *BazController
}
var builder, err = di.NewBuilder(
// provide autowired type with optional field Baz
di.Autowire((*Foo)(nil), di.Constraint("Baz", di.Optional(true))),
// provide constructor
di.Provide(NewBarController),
)
if err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
var container di.Container
if container, err = builder.Build(); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
err = container.Call(func(foo *Foo) error {
if foo == nil {
return errors.New("foo is nil")
}
if foo.Bar == nil {
return errors.New("foo.Bar is nil")
}
if foo.Baz != nil {
return errors.New("foo.Baz is not nil")
}
return nil
})
if err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
fmt.Println("Looks like something is fine")
// Output:
// Looks like something is fine
}
// This is example demonstrates how use dependency container for simple http application.
func Example_httpServer() {
var builder, err = di.NewBuilder(
di.BuilderOptions(
di.Autowire((*BarController)(nil), di.As((*Controller)(nil))), // provide autowired type
di.Autowire((*BazController)(nil), di.As((*Controller)(nil))), // provide autowired type
),
di.BuilderOptions(
di.Provide(NewServerMux), // provide constructor
di.Provide(NewServer), // provide constructor
),
)
if err != nil {
fmt.Println()
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
// build container
var container di.Container
if container, err = builder.Build(); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
// Call function with resolved dependencies
_ = container.Call(func(srv *http.Server) {
if srv == nil {
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
})
fmt.Println("Listening...")
// Output:
// Listening...
}