-
Notifications
You must be signed in to change notification settings - Fork 7
/
interface.go
281 lines (213 loc) · 7.35 KB
/
interface.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package gone
import (
"github.com/soheilhy/cmux"
"net"
"reflect"
"xorm.io/xorm"
)
//go:generate sh -c "mockgen -package=gone -self_package=github.com/gone-io/gone -source=interface.go -destination=mock_test.go"
// Goner which is an abstraction of injectable objects: can inject other Goner, can be injected by other Goner.
type Goner interface {
goneFlag()
}
type GonerOption interface {
option()
}
type Flag struct{}
func (g *Flag) goneFlag() {}
type identity interface {
GetId() GonerId
}
// GonerId Goner's id
type GonerId string
func (GonerId) option() {}
type Order int
func (Order) option() {}
const Order0 Order = 0
const Order1 Order = 10
const Order2 Order = 100
const Order3 Order = 1000
const Order4 Order = 10000
// Tomb container of Goner
type Tomb interface {
SetId(GonerId) Tomb
GetId() GonerId
GetGoner() Goner
GonerIsRevive(flags ...bool) bool
SetDefault(reflect.Type) Tomb
IsDefault(reflect.Type) bool
GetOrder() Order
SetOrder(order Order) Tomb
}
// Cemetery which is for burying and reviving Goner
type Cemetery interface {
Goner
// Bury a Goner to the Cemetery
Bury(Goner, ...GonerOption) Cemetery
// BuryOnce a Goner to the Cemetery, if the Goner is already in the Cemetery, it will be ignored
BuryOnce(goner Goner, options ...GonerOption) Cemetery
// ReplaceBury replace the Goner in the Cemetery with a new Goner
ReplaceBury(goner Goner, options ...GonerOption) error
// ReviveOne Revive a Goner from the Cemetery
ReviveOne(goner any) (deps []Tomb, err error)
// ReviveAllFromTombs Revive all Goner from the Cemetery
ReviveAllFromTombs() error
// GetTomById return the Tomb by the GonerId
GetTomById(GonerId) Tomb
// GetTomByType return the Tombs by the GonerType
GetTomByType(reflect.Type) []Tomb
// InjectFuncParameters used for inject func parameters,which will construct parameters for a func;return constructed parameters reflect.Value Slice;
// InjectFuncParameters accept two hook functions: injectBefore and injectAfter,and hook function can be nil.
// The ith parameter will be ignored by injecting process if injectBefore(x, i) returned is not nil, and the result of injectBefore(x, i) will be added to args.
InjectFuncParameters(
fn any,
injectBefore func(pt reflect.Type, i int) any,
injectAfter func(pt reflect.Type, i int),
) (args []reflect.Value, err error)
}
// Priest A function which has A Cemetery parameter, and return an error. use for Burying Goner
type Priest func(cemetery Cemetery) error
// Process a function which has a Cemetery parameter, and return an error. use for hooks
type Process func(cemetery Cemetery) error
type Heaven interface {
//Install do some prepare before start
Install() Heaven
//WaitEnd make program block until heaven stop
WaitEnd() Heaven
//End send a signal to heaven to stop
End() Heaven
//Start make heaven start
Start() Heaven
Stop() Heaven
//GetHeavenStopSignal return a channel to listen the signal of heaven stop
GetHeavenStopSignal() <-chan struct{}
//BeforeStart add a hook function which will execute before start;
BeforeStart(Process) Heaven
//AfterStart add a hook function which will execute after start
AfterStart(Process) Heaven
//BeforeStop add a hook function which will execute before stop
BeforeStop(Process) Heaven
//AfterStop add a hook function which will execute after stop
AfterStop(Process) Heaven
SetAfterStopSignalWaitSecond(sec int)
GetCemetery() Cemetery
}
type AfterReviveError error
// Prophet A interface which has a AfterRevive method
type Prophet interface {
Goner
//AfterRevive A method which will execute after revive
// Deprecate: use `AfterRevive() error` instead
AfterRevive() AfterReviveError
}
type Prophet2 interface {
Goner
//AfterRevive A method which will execute after revive
AfterRevive() error
}
type Angel interface {
Goner
Start(Cemetery) error
Stop(Cemetery) error
}
type SuckError error
type Vampire interface {
Goner
Suck(conf string, v reflect.Value) SuckError
}
type Vampire2 interface {
Goner
Suck(conf string, v reflect.Value, field reflect.StructField) error
}
// Provider is a factory function template, which return `T` instance and goner framework will call this function to
// create a new `T` instance for Type `T` fields of a struct who need injected.
// The parameter `tagConf` is the tag string of the field, and the parameter `param` should be an anonymous struct which field can be tag by `gone` and injected by goner framework.
// The function should be used for NewProviderPriest to create a provider priest.
type Provider[P, T any] func(tagConf string, param P) (T, error)
/*
Three errors:
- Internal error, internal system error, which can only be repaired by system upgrade.
- Parameter error, input error, error is caused by input information, and input needs to be adjusted.
- Business error, due to different business results guided by internal or external information
*/
// Error normal error
type Error interface {
error
Msg() string
Code() int
GetStatusCode() int
}
// InnerError which has stack, and which is used for Internal error
type InnerError interface {
Error
Stack() []byte
}
// BusinessError which has data, and which is used for Business error
type BusinessError interface {
Error
Data() any
}
// Logger log interface
type Logger interface {
Tracef(format string, args ...any)
Debugf(format string, args ...any)
Infof(format string, args ...any)
Printf(format string, args ...any)
Warnf(format string, args ...any)
Errorf(format string, args ...any)
Fatalf(format string, args ...any)
Panicf(format string, args ...any)
Trace(args ...any)
Debug(args ...any)
Info(args ...any)
Print(args ...any)
Warn(args ...any)
Error(args ...any)
Fatal(args ...any)
Panic(args ...any)
Traceln(args ...any)
Debugln(args ...any)
Infoln(args ...any)
Println(args ...any)
Warnln(args ...any)
Errorln(args ...any)
Fatalln(args ...any)
Panicln(args ...any)
}
// Tracer Log tracking, which is used to assign a unified traceId to the same call link to facilitate log tracking.
type Tracer interface {
//SetTraceId to set `traceId` to the calling function. If traceId is an empty string, an automatic one will
//be generated. TraceId can be obtained by using the GetTraceId () method in the calling function.
SetTraceId(traceId string, fn func())
//GetTraceId Get the traceId of the current goroutine
GetTraceId() string
//Go Start a new goroutine instead of `go func`, which can pass the traceid to the new goroutine.
Go(fn func())
//Recover use for catch panic in goroutine
Recover()
//RecoverSetTraceId SetTraceId and Recover
RecoverSetTraceId(traceId string, fn func())
}
type XormEngine interface {
xorm.EngineInterface
Transaction(fn func(session xorm.Interface) error) error
Sqlx(sql string, args ...any) *xorm.Session
GetOriginEngine() xorm.EngineInterface
SetPolicy(policy xorm.GroupPolicy)
}
//-----------
// CMuxServer cMux service,Used to multiplex the same port to listen for multiple protocols,ref:https://pkg.go.dev/github.com/soheilhy/cmux
type CMuxServer interface {
Match(matcher ...cmux.Matcher) net.Listener
MatchWithWriters(matcher ...cmux.MatchWriter) net.Listener
GetAddress() string
}
// -----------
// Configure use for get value of struct attribute tag by `gone:"config,${key}"`
type Configure interface {
//Get the value from config system
Get(key string, v any, defaultVal string) error
}
type NamedGoner interface {
GetGonerId() GonerId
}