-
Notifications
You must be signed in to change notification settings - Fork 3
/
godim.go
171 lines (152 loc) · 3.8 KB
/
godim.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
// Copyright 2018 ekino. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package godim
import (
"fmt"
"reflect"
)
// Godim is the main app controller
type Godim struct {
lifecycle *lifecycle
registry *Registry
configFunction func(key string, val reflect.Value) (interface{}, error)
eventSwitch *EventSwitch
}
// Default build a default Godim from default configuration
func Default() *Godim {
return DefaultConfig().Build()
}
// NewGodim build a Godim with specified Config
func NewGodim(config *Config) *Godim {
var g Godim
g.lifecycle = newLifecycle()
g.registry = newRegistryFromConfig(config)
g.configFunction = config.configFunction
if config.activateES {
g.eventSwitch = config.eventSwitch
}
return &g
}
// DeclareDefault : declare all your defaults services
func (godim *Godim) DeclareDefault(o ...interface{}) error {
if godim.lifecycle.current(stDeclaration) {
for _, v := range o {
err := godim.registry.declare(defaultStr, v)
if err != nil {
return newError(err).SetErrType(ErrTypeGodim)
}
}
} else {
return newError(fmt.Errorf("current phase %s", godim.lifecycle)).SetErrType(ErrTypeGodim)
}
return nil
}
// Declare specific level
func (godim *Godim) Declare(label string, o ...interface{}) error {
if godim.lifecycle.current(stDeclaration) {
for _, v := range o {
err := godim.registry.declare(label, v)
if err != nil {
return newError(err).SetErrType(ErrTypeGodim)
}
}
} else {
return newError(fmt.Errorf("current phase %s", godim.lifecycle)).SetErrType(ErrTypeGodim)
}
return nil
}
func (godim *Godim) configure() error {
if godim.lifecycle.current(stDeclaration) {
godim.lifecycle.currentState++
if godim.configFunction != nil {
err := godim.registry.configure(godim.configFunction)
if err != nil {
return err
}
}
}
return nil
}
func (godim *Godim) injection() error {
if godim.lifecycle.current(stConfiguration) {
godim.lifecycle.currentState++
err := godim.registry.injection()
if err != nil {
return err
}
}
return nil
}
func (godim *Godim) initialize() error {
if godim.lifecycle.current(stInjection) {
godim.lifecycle.currentState++
err := godim.registry.initializeAll()
if err != nil {
return err
}
}
return nil
}
//
// RunApp : Run the application after configuration and injection phase
//
func (godim *Godim) RunApp() error {
if godim.lifecycle.current(stRun) || godim.lifecycle.current(stClose) {
return newError(fmt.Errorf("Godim is already in state %s", godim.lifecycle)).SetErrType(ErrTypeGodim)
}
// Configuration phase
err := godim.configure()
if err != nil {
return err
}
// Injection phase
err = godim.injection()
if err != nil {
return err
}
// Initializer phase
err = godim.initialize()
if err != nil {
return err
}
// Run phase
if godim.lifecycle.current(stInitialization) {
godim.lifecycle.currentState++
if godim.eventSwitch != nil {
godim.eventSwitch.Start()
}
}
return nil
}
// CloseApp close all things declared in your app
func (godim *Godim) CloseApp() error {
if err := godim.closeIfRunning(); err != nil {
return err
}
if godim.eventSwitch != nil {
godim.eventSwitch.Close()
}
return nil
}
// CloseApp close all things declared in your app
func (godim *Godim) CloseAppGracefully() error {
if godim.eventSwitch != nil {
godim.eventSwitch.CloseGracefully()
}
return godim.closeIfRunning()
}
func (godim *Godim) closeIfRunning() error {
if godim.lifecycle.current(stRun) {
err := godim.registry.closeAll()
if err != nil {
return err
}
godim.lifecycle.currentState++
}
return nil
}
// GetStruct return the stored struct in case it is needed for other usage
func (godim *Godim) GetStruct(label, key string) interface{} {
return godim.registry.getElement(label, key)
}