This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
enum.go
63 lines (58 loc) · 1.88 KB
/
enum.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
package lcs
import (
"reflect"
)
var regEnumTypeToIdx map[reflect.Type]map[reflect.Type]EnumKeyType
var regEnumIdxToType map[reflect.Type][]reflect.Type
// RegisterEnum register an enum type with its available variants. If the enum type
// was registered, it will be overwriten.
//
// This function panics on errors. The returned error is always nil.
func RegisterEnum(enumTypePtr interface{}, types ...interface{}) (err error) {
rEnumType := reflect.TypeOf(enumTypePtr)
if rEnumType.Kind() != reflect.Ptr {
panic("enumType should be a pointer to a nil interface")
}
rEnumType = rEnumType.Elem()
if rEnumType.Kind() != reflect.Interface {
panic("enumType should be a pointer to a nil interface")
}
if regEnumTypeToIdx == nil {
regEnumTypeToIdx = make(map[reflect.Type]map[reflect.Type]uint64)
}
if regEnumIdxToType == nil {
regEnumIdxToType = make(map[reflect.Type][]reflect.Type)
}
regEnumIdxToType[rEnumType] = make([]reflect.Type, 0, len(types))
regEnumTypeToIdx[rEnumType] = make(map[reflect.Type]uint64)
for i, t := range types {
rType := reflect.TypeOf(t)
if !rType.Implements(rEnumType) {
panic(rType.String() + " does not implement " + rEnumType.String())
}
regEnumIdxToType[rEnumType] = append(regEnumIdxToType[rEnumType], rType)
regEnumTypeToIdx[rEnumType][rType] = EnumKeyType(i)
}
// log.Printf("registered: %v", rEnumType.String())
return
}
func enumGetTypeByIdx(enumType reflect.Type, idx EnumKeyType) (reflect.Type, bool) {
// log.Printf("enumGetTypeByIdx: %v", enumType.String())
m, ok := regEnumIdxToType[enumType]
if !ok {
return nil, false
}
if len(m) <= int(idx) {
return nil, false
}
return m[int(idx)], true
}
func enumGetIdxByType(enumType, vType reflect.Type) (EnumKeyType, bool) {
// log.Printf("enumGetIdxByType: %v", enumType.String())
m, ok := regEnumTypeToIdx[enumType]
if !ok {
return 0, false
}
idx, ok := m[vType]
return idx, ok
}