-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeHintMap.go
81 lines (69 loc) · 1.8 KB
/
typeHintMap.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
package eventuate
import (
"reflect"
"strings"
)
type typeHintsMap map[string]reflect.Type
func NewTypeHintsMap() typeHintsMap {
return make(map[string]reflect.Type)
}
func (hints typeHintsMap) Merge(newHints typeHintsMap) {
for k, v := range newHints {
hints[k] = v
}
}
func (hints typeHintsMap) MakeCopy() typeHintsMap {
result := make(map[string]reflect.Type)
for k, v := range hints {
result[k] = v
}
return result
}
func (hints typeHintsMap) HasEventType(name string) bool {
_, hasType := hints[name]
return hasType
}
func (hints typeHintsMap) GetEventType(name string) reflect.Type {
result, hasType := hints[name]
if hasType {
return result
}
return nil
}
func (hints typeHintsMap) GetTypeByKeyName(name string) (bool, reflect.Type) {
result, hasIt := hints[name]
return hasIt, result
}
func (hints typeHintsMap) GetTypeByTypeName(typeName string) (bool, string) {
for mk, mv := range hints {
if mv.Name() == typeName {
return true, mk
}
//log.Printf("Type %s (%s): nope", mv.Name(), getUnderlyingType(mv).Name())
str := getUnderlyingType(mv).Name()
if lastPart(typeName, ".") == mv.Name() {
return true, mk
}
if lastPart(typeName, ".") == str {
return true, mk
}
}
return false, ""
}
func (hints typeHintsMap) RegisterEventType(name string, typeInstance interface{}) error {
argType := reflect.TypeOf(typeInstance)
underlyingType := getUnderlyingType(argType)
if underlyingType.Kind() != reflect.Struct {
return AppError("Type hint `%s` requires a type (`%T`) which is not ultimately a structure",
name,
underlyingType)
}
classOk := strings.HasSuffix(underlyingType.Name(), "Event")
if !classOk {
return AppError("Type hint `%s` requires a structure (`%T`) named with ending 'Event'",
name,
underlyingType)
}
hints[name] = argType
return nil
}