-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
162 lines (130 loc) · 3.94 KB
/
example_test.go
File metadata and controls
162 lines (130 loc) · 3.94 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
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
package wirejacket_test
import (
"fmt"
"log"
wirejacket "github.com/bang9211/wire-jacket"
)
// ==============================================
// Database Interface - MockupDB Implement example
// ==============================================
type Database interface {
// Connect DB.
Connect() error
// Close closes the REST API Server.
Close() error
}
type MockupDB struct{}
func NewMockupDB() Database {
return &MockupDB{}
}
func (mdb *MockupDB) Connect() error {
return nil
}
func (mdb *MockupDB) Close() error {
return nil
}
// =========================================================
// Blockchain Interface - MockupBlockchain Implement example
// =========================================================
type Block interface {
GetData() string
}
type MockupBlock struct {
data string
}
func (mb *MockupBlock) GetData() string {
return mb.data
}
type Blockchain interface {
// Init inits blockchain.
Init() error
// AddBlock adds data to blockchain.
AddBlock(data string) error
// GetBlocks gets all the blocks.
GetBlocks() []Block
// Close closes blockchain.
Close() error
}
var genesisBlockData = "Genesis Block Data"
type MockupBlockchain struct {
db Database
blocks []Block
}
func NewMockupBlockchain(db Database) Blockchain {
return &MockupBlockchain{db: db, blocks: []Block{}}
}
func (mbc *MockupBlockchain) Init() error {
mbc.db.Connect()
mbc.AddBlock(genesisBlockData)
return nil
}
func (mbc *MockupBlockchain) AddBlock(data string) error {
mbc.blocks = append(mbc.blocks, &MockupBlock{data: data})
return nil
}
func (mbc *MockupBlockchain) GetBlocks() []Block {
return mbc.blocks
}
func (mbc *MockupBlockchain) Close() error {
return nil
}
// =======================================
// wire_gen.go(Wire generated code) example
// =======================================
// InjectMockupDB injects dependencies and inits of Database.
func InjectMockupDB() (Database, error) {
database := NewMockupDB()
return database, nil
}
// InjectMockupBlockchain injects dependencies and inits of Blockchain.
func InjectMockupBlockchain(db Database) (Blockchain, error) {
blockchain := NewMockupBlockchain(db)
return blockchain, nil
}
var Injectors = map[string]interface{}{
"mockup_database": InjectMockupDB,
}
var EagerInjectors = map[string]interface{}{
"mockup_blockchain": InjectMockupBlockchain,
}
// ==================
// User code examples
// ==================
// Default use case to use New().
// Wire-Jacket defaultly uses 'app.conf' for setting modules
// to activate. Or you can use the flag '--config {file_name}'.
func Example() {
// Create wirejacket and set injectors.
wj := wirejacket.New().
SetEagerInjectors(EagerInjectors).
SetInjectors(Injectors)
defer wj.Close()
// Inject eager injectors.
if err := wj.DoWire(); err != nil {
// Error occured in this example, because there is no app.conf and modules.
log.Print(err)
}
// You can set modules using SetActivatingModules() directly without app.conf.
wj.SetActivatingModules([]string{"mockup_blockchain", "mockup_database"})
// Get module and use.
blockchain := wj.GetModule("mockup_blockchain").(Blockchain)
blockchain.AddBlock("test")
fmt.Println(blockchain.GetBlocks())
}
// Second use case to use NewWithServiceName().
func Example_second() {
// Create wirejacket with serviceName.
wj := wirejacket.NewWithServiceName("example_service")
defer wj.Close()
// You can also add injector directly, instead of SetInjectors().
wj.AddInjector("mockup_database", InjectMockupDB)
wj.AddEagerInjector("mockup_blockchain", InjectMockupBlockchain)
// Check value of modules in app.conf.
// Or You can set modules using SetActivatingModules() directly.
wj.SetActivatingModules([]string{"mockup_blockchain", "mockup_database"})
// You can also get module without DoWire().
// the dependencies of the module are injected automatically.
blockchain := wj.GetModule("mockup_blockchain").(Blockchain)
blockchain.AddBlock("test")
fmt.Println(blockchain.GetBlocks())
}