-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
132 lines (109 loc) · 2.75 KB
/
Copy pathexample_test.go
File metadata and controls
132 lines (109 loc) · 2.75 KB
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
package oak_test
import (
"context"
"fmt"
"github.com/ARTM2000/oak"
)
// Types used in examples only.
type Logger struct{ Prefix string }
type Config struct{ DSN string }
type Database struct {
Config *Config
Logger *Logger
}
type Greeter interface {
Greet() string
}
type englishGreeter struct{}
func (g *englishGreeter) Greet() string { return "hello" }
type spanishGreeter struct{}
func (g *spanishGreeter) Greet() string { return "hola" }
func ExampleNew() {
c := oak.New()
_ = c.Register(func() *Logger { return &Logger{Prefix: "app"} })
if err := c.Build(); err != nil {
panic(err)
}
logger, _ := oak.Resolve[*Logger](c)
fmt.Println(logger.Prefix)
// Output: app
}
func ExampleWithLifetime() {
c := oak.New()
_ = c.Register(
func() *Logger { return &Logger{Prefix: "app"} },
oak.WithLifetime(oak.Transient),
)
_ = c.Build()
l1, _ := oak.Resolve[*Logger](c)
l2, _ := oak.Resolve[*Logger](c)
fmt.Println(l1 == l2)
// Output: false
}
func ExampleResolve() {
c := oak.New()
_ = c.Register(func() *Config { return &Config{DSN: "postgres://localhost"} })
_ = c.Register(func() *Logger { return &Logger{Prefix: "app"} })
_ = c.Register(func(cfg *Config, log *Logger) *Database {
return &Database{Config: cfg, Logger: log}
})
_ = c.Build()
db, err := oak.Resolve[*Database](c)
if err != nil {
panic(err)
}
fmt.Println(db.Config.DSN)
fmt.Println(db.Logger.Prefix)
// Output:
// postgres://localhost
// app
}
func ExampleContainer_RegisterNamed() {
c := oak.New()
_ = c.RegisterNamed("dev", func() *Config { return &Config{DSN: "localhost"} })
_ = c.RegisterNamed("prod", func() *Config { return &Config{DSN: "prod-host"} })
_ = c.Build()
dev, _ := oak.ResolveNamed[*Config](c, "dev")
prod, _ := oak.ResolveNamed[*Config](c, "prod")
fmt.Println(dev.DSN)
fmt.Println(prod.DSN)
// Output:
// localhost
// prod-host
}
func ExampleResolveNamed() {
c := oak.New()
_ = c.RegisterNamed("en", func() Greeter { return &englishGreeter{} })
_ = c.RegisterNamed("es", func() Greeter { return &spanishGreeter{} })
_ = c.Build()
en, _ := oak.ResolveNamed[Greeter](c, "en")
es, _ := oak.ResolveNamed[Greeter](c, "es")
fmt.Println(en.Greet())
fmt.Println(es.Greet())
// Output:
// hello
// hola
}
// connPool implements io.Closer, so oak.Shutdown will call Close automatically.
type connPool struct {
DSN string
closed bool
}
func (p *connPool) Close() error {
p.closed = true
return nil
}
func ExampleContainer_Shutdown() {
c := oak.New()
_ = c.Register(func() *connPool {
return &connPool{DSN: "postgres://localhost"}
})
_ = c.Build()
pool, _ := oak.Resolve[*connPool](c)
fmt.Println("before:", pool.closed)
_ = c.Shutdown(context.Background())
fmt.Println("after:", pool.closed)
// Output:
// before: false
// after: true
}