-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelp.go
368 lines (312 loc) · 9.51 KB
/
help.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package gone
import (
"bytes"
"fmt"
"reflect"
"runtime"
"strings"
"time"
)
// GonerIds for Gone framework inner Goners
const (
// IdGoneHeaven , The GonerId of Heaven Goner, which represents the program itself, and which is injected by default when it starts.
IdGoneHeaven GonerId = "gone-heaven"
// IdGoneCemetery , The GonerId of Cemetery Goner, which is Dependence Injection Key Goner, and which is injected by default.
IdGoneCemetery GonerId = "gone-cemetery"
// IdGoneTestKit , The GonerId of TestKit Goner, which is injected by default when using gone.Test or gone.TestAt to run test code.
IdGoneTestKit GonerId = "gone-test-kit"
//IdConfig , The GonerId of Config Goner, which can be used for Injecting Configs from files or envs.
IdConfig GonerId = "config"
//IdGoneConfigure , The GonerId of Configure Goner, which is used to read configs from devices.
IdGoneConfigure GonerId = "gone-configure"
// IdGoneTracer ,The GonerId of Tracer
IdGoneTracer GonerId = "gone-tracer"
// IdGoneLogger , The GonerId of Logger
IdGoneLogger GonerId = "gone-logger"
// IdGoneCMux , The GonerId of CMuxServer
IdGoneCMux GonerId = "gone-cmux"
// IdGoneGin , IdGoneGinRouter , IdGoneGinProcessor, IdGoneGinProxy, IdGoneGinResponser, IdHttpInjector;
// The GonerIds of Goners in goner/gin, which integrates gin framework for web request.
IdGoneGin GonerId = "gone-gin"
IdGoneGinRouter GonerId = "gone-gin-router"
IdGoneGinSysMiddleware GonerId = "gone-gin-sys-middleware"
IdGoneGinProxy GonerId = "gone-gin-proxy"
IdGoneGinResponser GonerId = "gone-gin-responser"
IdHttpInjector GonerId = "http"
// IdGoneXorm , The GonerId of XormEngine Goner, which is for xorm engine.
IdGoneXorm GonerId = "gone-xorm"
// IdGoneRedisPool ,IdGoneRedisCache, IdGoneRedisKey, IdGoneRedisLocker, IdGoneRedisProvider
// The GonerIds of Goners in goner/redis, which integrates redis framework for cache and locker.
IdGoneRedisPool GonerId = "gone-redis-pool"
IdGoneRedisCache GonerId = "gone-redis-cache"
IdGoneRedisKey GonerId = "gone-redis-key"
IdGoneRedisLocker GonerId = "gone-redis-locker"
IdGoneRedisProvider GonerId = "gone-redis-provider"
// IdGoneSchedule , The GonerId of Schedule Goner, which is for schedule in goner/schedule.
IdGoneSchedule GonerId = "gone-schedule"
// IdGoneReq , The GonerId of urllib.Client Goner, which is for request in goner/urllib.
IdGoneReq GonerId = "gone-urllib"
)
const (
RequestIdHeaderKey = "X-Request-Id"
TraceIdHeaderKey = "X-Trace-Id"
)
// PanicTrace used for getting panic stack
func PanicTrace(kb int, skip int) []byte {
stack := make([]byte, kb<<10) //4KB
length := runtime.Stack(stack, true)
_, filename, fileLine, ok := runtime.Caller(skip)
start := 0
if ok {
start = bytes.Index(stack, []byte(fmt.Sprintf("%s:%d", filename, fileLine)))
stack = stack[start:length]
}
line := []byte("\n")
start = bytes.Index(stack, line) + 1
stack = stack[start:]
end := bytes.LastIndex(stack, line)
if end != -1 {
stack = stack[:end]
}
e := []byte("\ngoroutine ")
end = bytes.Index(stack, e)
if end != -1 {
stack = stack[:end]
}
stack = bytes.TrimRight(stack, "\n")
return stack
}
// GetFuncName get function name
func GetFuncName(f any) string {
return strings.TrimSuffix(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), "-fm")
}
// GetInterfaceType get interface type
func GetInterfaceType[T any](t *T) reflect.Type {
return reflect.TypeOf(t).Elem()
}
type defaultType struct {
t reflect.Type
}
func (d defaultType) option() {}
func IsDefault[T any](t *T) GonerOption {
return defaultType{t: GetInterfaceType(t)}
}
type provideType struct {
t []reflect.Type
}
func (d provideType) option() {}
// Provide a kind of GonerOption, which can be used in burying Vampire2(which is implemented `Suck(conf string, v reflect.Value, field reflect.StructField) error`) to framework.
// Provide will get the type of the object and put it in the GonerOption. When A goner need to be injected,
// the framework will find the Vampire2 which Tag by the GonerOption and call the Suck method to create the goner and inject it.
func Provide(objs ...any) GonerOption {
m := make(map[reflect.Type]any)
for _, o := range objs {
m[reflect.TypeOf(o)] = o
}
p := provideType{
t: make([]reflect.Type, 0),
}
for o, _ := range m {
p.t = append(p.t, o)
}
return p
}
// WrapNormalFnToProcess warp a func to Process
func WrapNormalFnToProcess(fn any) Process {
return func(cemetery Cemetery) error {
args, err := cemetery.InjectFuncParameters(fn, nil, nil)
if err != nil {
return err
}
results := reflect.ValueOf(fn).Call(args)
for _, result := range results {
if err, ok := result.Interface().(error); ok {
return err
}
}
return nil
}
}
// IsCompatible t Type can put in goner
func IsCompatible(t reflect.Type, goner any) bool {
gonerType := reflect.TypeOf(goner)
switch t.Kind() {
case reflect.Interface:
return gonerType.Implements(t)
case reflect.Struct:
return gonerType.Elem() == t
default:
return gonerType == t
}
}
func setFieldValue(v reflect.Value, ref any) {
t := v.Type()
switch t.Kind() {
case reflect.Interface, reflect.Pointer, reflect.Slice, reflect.Map:
v.Set(reflect.ValueOf(ref))
default:
v.Set(reflect.ValueOf(ref).Elem())
}
return
}
type timeUseRecord struct {
UseTime time.Duration
Count int64
}
var mapRecord = make(map[string]*timeUseRecord)
// TimeStat record the time of function and avg time
func TimeStat(name string, start time.Time, logs ...func(format string, args ...any)) {
since := time.Since(start)
if mapRecord[name] == nil {
mapRecord[name] = &timeUseRecord{}
}
mapRecord[name].UseTime += since
mapRecord[name].Count++
var log func(format string, args ...any)
if len(logs) == 0 {
log = func(format string, args ...any) {
fmt.Printf(format, args...)
}
} else {
log = logs[0]
}
log("%s executed %v times, took %v, avg: %v\n",
name,
mapRecord[name].Count,
mapRecord[name].UseTime,
mapRecord[name].UseTime/time.Duration(mapRecord[name].Count),
)
}
func RunTest(fn any, priests ...Priest) {
Prepare(priests...).testKit().Run(fn)
}
// Test Use for writing test cases, refer to [example](https://github.com/gone-io/gone/blob/main/example/test/goner_test.go)
func Test[T Goner](fn func(goner T), priests ...Priest) {
RunTest(func(in struct {
cemetery Cemetery `gone:"*"`
}) {
ft := reflect.TypeOf(fn)
t := ft.In(0).Elem()
theTombs := in.cemetery.GetTomByType(t)
if len(theTombs) == 0 {
panic(CannotFoundGonerByTypeError(t))
}
fn(theTombs[0].GetGoner().(T))
}, priests...)
}
// TestAt Use for writing test cases, test a specific ID of Goner
func TestAt[T Goner](id GonerId, fn func(goner T), priests ...Priest) {
RunTest(func(in struct {
cemetery Cemetery `gone:"*"`
}) {
theTomb := in.cemetery.GetTomById(id)
if theTomb == nil {
panic(CannotFoundGonerByIdError(id))
}
g, ok := theTomb.GetGoner().(T)
if !ok {
panic(NotCompatibleError(reflect.TypeOf(g).Elem(), reflect.TypeOf(theTomb.GetGoner()).Elem()))
}
fn(g)
}, priests...)
}
// NewBuryMockCemeteryForTest make a new Cemetery for test
func NewBuryMockCemeteryForTest() Cemetery {
return newCemetery()
}
func (p *Preparer) testKit() *Preparer {
type Kit struct {
Flag
}
p.heaven.(*heaven).cemetery.Bury(&Kit{}, IdGoneTestKit)
return p
}
/*
Test Use for writing test cases
example:
```go
gone.Prepare(priests...).Test(func(in struct{
cemetery Cemetery `gone:"*"`
}) {
// test code
})
```
*/
func (p *Preparer) Test(fn any) {
p.testKit().AfterStart(fn).Run()
}
// TagStringParse parse tag string to map
// example: "a=1,b=2" -> map[string]string{"a":"1","b":"2"}
func TagStringParse(conf string) map[string]string {
conf = strings.TrimSpace(conf)
specs := strings.Split(conf, ",")
m := make(map[string]string)
for _, spec := range specs {
spec = strings.TrimSpace(spec)
pairs := strings.Split(spec, "=")
if len(pairs) == 1 && pairs[0] != "" {
m[pairs[0]] = ""
} else if len(pairs) > 1 && pairs[0] != "" {
m[pairs[0]] = pairs[1]
}
}
return m
}
type provider[T any] struct {
Flag
cemetery Cemetery `gone:"*"`
create func(tagConf string) (T, error)
}
func (p *provider[T]) Suck(conf string, v reflect.Value, field reflect.StructField) error {
obj, err := p.create(conf)
if err != nil {
return ToError(err)
}
v.Set(reflect.ValueOf(obj))
return nil
}
// NewProviderPriest create a provider priest function for goner from a function like: `func(tagConf string, injectableStructParam struct{}) (provideType T, err error)`
// example:
// ```go
// type MyGoner struct {}
//
// func NewMyGoner(tagConf string, param struct{
// depGoner1 MyGoner1 `gone:"*"` // inject dep
// depGoner2 MyGoner2 `gone:"*"` // inject dep
// configStr string `gone:"config,my.config.str"` // inject config from config file
// }) (MyGoner, error) {
//
// // do something
// return MyGoner{}, nil
// }
//
// var priest = NewProviderPriest(NewMyGoner)
// ```
func NewProviderPriest[P, T any](fn Provider[P, T]) Priest {
p := provider[T]{}
t := new(T)
of := reflect.TypeOf(t).Elem()
pt := provideType{
t: []reflect.Type{of},
}
p.create = func(tagConf string) (T, error) {
args, err := p.cemetery.InjectFuncParameters(fn, func(pt reflect.Type, i int) any {
if i == 0 {
return tagConf
}
return nil
}, nil)
if err != nil {
return *new(T), err
}
results := reflect.ValueOf(fn).Call(args)
if results[1].IsNil() {
return results[0].Interface().(T), nil
}
return *new(T), ToError(results[1].Interface())
}
return func(cemetery Cemetery) error {
cemetery.Bury(&p, pt)
return nil
}
}