-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.go
More file actions
70 lines (57 loc) · 1.65 KB
/
echo.go
File metadata and controls
70 lines (57 loc) · 1.65 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
package ctxecho
import (
"reflect"
"unsafe"
)
const (
keyFieldname = "key"
valFieldname = "val"
)
// Context values are associated with a context and may have key, value
// fields associated with it. The value field can be ANY (interface{})
// type as well as the key. Returns a map of key, value pairs. The key
// is the name of the field in the context. The value is the value of
// the field.
func Inspect(ctx any) map[any]any {
store := make(map[any]any)
inspect(store, ctx)
return store
}
func inspect(store map[any]any, ctx any) {
contextValues := reflect.ValueOf(ctx)
contextKeys := reflect.TypeOf(ctx)
if reflect.TypeOf(ctx).Kind() == reflect.Ptr {
contextValues = contextValues.Elem()
contextKeys = contextKeys.Elem()
}
if contextKeys.Kind() != reflect.Struct {
return
}
iterateCtx(store, contextValues, contextKeys)
}
func iterateCtx(store map[any]any, values reflect.Value, types reflect.Type) {
var keyName any
for i := 0; i < values.NumField(); i++ {
fieldValue := values.Field(i)
fieldName := types.Field(i).Name
if fieldName == keyFieldname || fieldName == valFieldname {
fieldValue = reflectNewAt(fieldValue)
if fieldName == keyFieldname {
keyName = fieldValue.Interface()
} else {
store[keyName] = fieldValue.Interface()
}
continue
}
switch fieldName {
case "Context":
inspect(store, fieldValue.Interface())
case "cancelCtx", "timerCtx", "valueCtx":
fieldValue = reflectNewAt(fieldValue)
inspect(store, fieldValue.Interface())
}
}
}
func reflectNewAt(fieldValue reflect.Value) reflect.Value {
return reflect.NewAt(fieldValue.Type(), unsafe.Pointer(fieldValue.UnsafeAddr())).Elem()
}