-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.go
More file actions
86 lines (67 loc) · 1.8 KB
/
helper.go
File metadata and controls
86 lines (67 loc) · 1.8 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package observer
import (
"fmt"
"reflect"
)
func checkObserver(observer interface{}, fieldName string) (reflect.Type, string, reflect.Value) {
t, topic, fc := checkObserverForInterface(observer)
if len(topic) > 0 && !fc.IsZero() {
return t, topic, fc
}
// 字段名
field, ok := t.FieldByName(fieldName)
if !ok {
panic(fmt.Sprintf("%s is no %s field", t.String(), fieldName))
}
if len(topic) <= 0 {
topic, ok = field.Tag.Lookup("topic")
}
if !ok {
panic(fmt.Sprintf("%s.%s field is no topic in the tag", t.String(), fieldName))
}
if !fc.IsZero() {
return t, topic, fc
}
function, ok := field.Tag.Lookup("notice")
if !ok {
panic(fmt.Sprintf("%s.%s field is no notice in the tag", t.String(), fieldName))
}
fc = reflect.ValueOf(observer).MethodByName(function)
if fc.IsZero() {
panic(fmt.Sprintf("%s does not exist in %s", function, t.String()))
}
return t, topic, fc
}
func checkObserverForInterface(observer interface{}) (reflect.Type, string, reflect.Value) {
t := reflect.TypeOf(observer)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
panic(fmt.Sprintf("%s is not reflect.Struct", t.String()))
}
var topic, function string
topicf, ok := observer.(Topic)
if ok {
topic = topicf.Topic()
}
functionf, ok := observer.(Function)
if ok {
function = functionf.Function()
}
if len(function) <= 0 {
return t, topic, reflect.Zero(reflect.TypeOf(observer))
}
fc := reflect.ValueOf(observer).MethodByName(function)
if fc.IsZero() {
panic(fmt.Sprintf("%s does not exist in %s", function, t.String()))
}
return t, topic, fc
}
func checkFunc(fc interface{}) (reflect.Type, reflect.Value) {
t := reflect.TypeOf(fc)
if t.Kind() != reflect.Func {
panic(fmt.Sprintf("%s is not reflect.Func", t.String()))
}
return t, reflect.ValueOf(fc)
}