-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_1_test.go
70 lines (55 loc) · 1.05 KB
/
sample_1_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
package sample
import (
"fmt"
"os"
"github.com/pavelmemory/fig"
)
type UserRepo interface {
Find() []string
}
type MemUserRepo struct {
message string
}
func (mur *MemUserRepo) Find() []string {
return []string{"mem", mur.message}
}
type FileUserRepo struct {
message string
}
func (fur *FileUserRepo) Find() []string {
return []string{"file", fur.message}
}
type OrderRepo struct {
}
func (or *OrderRepo) Create() string {
return "order created"
}
type Module struct {
Name string `fig:"env[ENV_NAME]"`
OrderRepo *OrderRepo
UserRepo
}
func init() {
os.Setenv("ENV_NAME", "DEV")
}
func ExampleOfSimpleInjectionOnDependenciesAndEnvVar() {
injector := fig.New(false)
err := injector.Register(
&MemUserRepo{message: "mes_1"},
)
if err != nil {
panic(fmt.Sprintf("%#v", err))
}
module := new(Module)
err = injector.Initialize(module)
if err != nil {
panic(fmt.Sprintf("%#v", err))
}
fmt.Println(module.Name)
fmt.Println(module.Find())
fmt.Println(module.OrderRepo.Create())
// Output:
//DEV
//[mem mes_1]
//order created
}