This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customType.go
116 lines (105 loc) · 2.98 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
package types
import (
"github.com/freehere107/go-scale-codec/source"
"github.com/freehere107/go-scale-codec/utiles"
"regexp"
"strings"
)
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, typeStruct := range registry {
key = strings.ToLower(key)
switch typeStruct.Type {
case "string":
typeString := typeStruct.TypeString
instant := typeRegistry[strings.ToLower(typeString)]
if instant != nil {
regCustomKey(key, instant)
continue
}
// Explained
if explainedType, ok := registry[typeString]; ok {
if explainedType.Type == "string" {
instant := typeRegistry[strings.ToLower(explainedType.TypeString)]
if instant != nil {
regCustomKey(key, instant)
continue
}
} else {
RegCustomTypes(map[string]source.TypeStruct{key: registry[typeString]})
}
}
// sub type vec|option
if typeString[len(typeString)-1:] == ">" {
reg := regexp.MustCompile("^([^<]*)<(.+)>$")
typeParts := reg.FindStringSubmatch(typeString)
if len(typeParts) > 2 {
if strings.ToLower(typeParts[1]) == "vec" {
v := Vec{}
v.SubType = typeParts[2]
regCustomKey(key, &v)
continue
} else if strings.ToLower(typeParts[1]) == "option" {
v := Option{}
v.SubType = typeParts[2]
regCustomKey(key, &v)
continue
}
}
}
// Tuple
if typeString != "()" && string(typeString[0]) == "(" && string(typeString[len(typeString)-1:]) == ")" {
s := Struct{}
s.TypeString = typeString
s.buildStruct()
regCustomKey(key, &s)
}
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)
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)
case "set":
regCustomKey(key, &Set{ValueList: typeStruct.ValueList})
}
}
}
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])
}
}
if specialRegistry == nil {
specialRegistry = make(map[string]Special)
}
specialRegistry[slice[0]] = special
} else {
typeRegistry[key] = rt
}
}