-
Notifications
You must be signed in to change notification settings - Fork 36
/
customType.go
179 lines (167 loc) · 4.73 KB
/
customType.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
package types
import (
"fmt"
"regexp"
"strings"
"github.com/itering/scale.go/source"
"github.com/itering/scale.go/types/convert"
"github.com/itering/scale.go/utiles"
)
func newStruct(names, typeString []string) *TypeMapping {
if len(names) != len(typeString) {
panic("init newStruct names and typeString length not equal")
}
if len(names) == 0 {
return nil
}
return &TypeMapping{Names: names, Types: typeString}
}
func RegCustomTypes(registry map[string]source.TypeStruct) {
for key := range registry {
typeStruct := registry[key]
if typeStruct.V14 {
V14TypesLock.RLock()
_, ok := V14Types[key]
V14TypesLock.RUnlock()
if !ok {
V14TypesLock.Lock()
V14Types[key] = typeStruct
V14TypesLock.Unlock()
} else {
if typeStruct.SpecVec == 0 || EqTypeStringWithTypeStruct(key, &typeStruct) {
return
}
// history spec type
key = fmt.Sprintf("%s#%d-%d", key, typeStruct.SpecVec, typeStruct.SpecVec)
}
}
key = strings.ToLower(key)
switch typeStruct.Type {
case "string":
typeString := convert.ConvertType(typeStruct.TypeString)
TypeRegistryLock.RLock()
instant := TypeRegistry[strings.ToLower(typeString)]
TypeRegistryLock.RUnlock()
if instant != nil {
regCustomKey(key, instant)
continue
}
// Explained
if explainedType, ok := registry[typeString]; ok {
if explainedType.Type == "string" {
TypeRegistryLock.RLock()
instant := TypeRegistry[strings.ToLower(explainedType.TypeString)]
TypeRegistryLock.RUnlock()
if instant != nil {
regCustomKey(key, instant)
continue
}
} else {
RegCustomTypes(map[string]source.TypeStruct{key: registry[typeString]})
}
}
// subtype vec|option
if typeString[len(typeString)-1:] == ">" {
reg := regexp.MustCompile("^([^<]*)<(.+)>$")
typeParts := reg.FindStringSubmatch(typeString)
if len(typeParts) > 2 {
if strings.EqualFold(typeParts[1], "vec") {
v := Vec{}
v.SubType = typeParts[2]
regCustomKey(key, &v)
continue
} else if strings.EqualFold(typeParts[1], "option") {
v := Option{}
v.SubType = typeParts[2]
regCustomKey(key, &v)
continue
} else if strings.EqualFold(typeParts[1], "compact") {
v := Compact{}
v.SubType = typeParts[2]
regCustomKey(key, &v)
continue
} else if strings.EqualFold(typeParts[1], "BTreeMap") {
v := BTreeMap{}
v.SubType = typeParts[2]
regCustomKey(key, &v)
continue
} else if strings.EqualFold(typeParts[1], "BTreeSet") {
v := BTreeSet{}
v.SubType = typeParts[2]
regCustomKey(key, &v)
continue
}
}
}
// Tuple
if typeString != "()" && string(typeString[0]) == "(" && typeString[len(typeString)-1:] == ")" {
s := Struct{}
s.TypeString = typeString
s.buildStruct()
regCustomKey(key, &s)
continue
}
// Array
if typeString != "[]" && string(typeString[0]) == "[" && typeString[len(typeString)-1:] == "]" {
if typePart := strings.Split(typeString[1:len(typeString)-1], ";"); len(typePart) == 2 {
fixed := FixedArray{
FixedLength: utiles.StringToInt(strings.TrimSpace(typePart[1])),
SubType: strings.TrimSpace(typePart[0]),
}
regCustomKey(key, &fixed)
continue
}
}
case "struct":
var names, typeStrings []string
for _, v := range typeStruct.TypeMapping {
names = append(names, v[0])
typeStrings = append(typeStrings, v[1])
}
s := Struct{}
s.TypeMapping = newStruct(names, typeStrings)
regCustomKey(key, &s)
continue
case "enum":
var names, typeStrings []string
for _, v := range typeStruct.TypeMapping {
names = append(names, v[0])
typeStrings = append(typeStrings, v[1])
}
e := Enum{ValueList: typeStruct.ValueList}
e.TypeMapping = newStruct(names, typeStrings)
regCustomKey(key, &e)
continue
case "set":
regCustomKey(key, &Set{ValueList: typeStruct.ValueList, BitLength: typeStruct.BitLength})
continue
}
}
}
func regCustomKey(key string, rt interface{}) {
slice := strings.Split(key, "#")
if len(slice) == 2 { // for Special
special := Special{Registry: rt, Version: []int{0, 99999999}}
if version := strings.Split(slice[1], "-"); len(version) == 2 {
special.Version[0] = utiles.StringToInt(version[0])
if version[1] != "?" {
special.Version[1] = utiles.StringToInt(version[1])
}
}
specialRegistryLock.RLock()
instant, ok := specialRegistry[slice[0]]
specialRegistryLock.RUnlock()
if ok {
instant = append(instant, special)
} else {
instant = []Special{special}
}
specialRegistryLock.Lock()
specialRegistry[slice[0]] = instant
specialRegistryLock.Unlock()
} else {
TypeRegistryLock.Lock()
TypeRegistry[key] = rt
TypeRegistryLock.Unlock()
}
}