-
Notifications
You must be signed in to change notification settings - Fork 6
/
help.go
261 lines (217 loc) · 6.76 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
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)}
}
// 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()
}