forked from itering/scale.go
/
base.go
173 lines (147 loc) · 4.16 KB
/
base.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
package types
import (
"fmt"
"reflect"
"regexp"
"strings"
"github.com/kaigedong/scale.go/utiles"
)
type ScaleDecoderOption struct {
Spec int
SubType string
Module string
ValueList []string
Metadata *MetadataStruct
FixedLength int
SignedExtensions []SignedExtension `json:"signed_extensions"`
TypeName string
}
type TypeMapping struct {
Names []string
Types []string
}
type IScaleDecoder interface {
Init(data ScaleBytes, option *ScaleDecoderOption)
Process()
Encode(interface{}) string
buildStruct()
NextBytes(int) []byte
GetNextU8() int
reset()
}
type ScaleDecoder struct {
Data ScaleBytes `json:"-"`
TypeString string `json:"-"`
SubType string `json:"-"`
Value interface{} `json:"-"`
RawValue string `json:"-"`
TypeMapping *TypeMapping `json:"-"`
Metadata *MetadataStruct `json:"-"`
Spec int `json:"-"`
Module string `json:"-"`
TypeName string
}
func (s *ScaleDecoder) Init(data ScaleBytes, option *ScaleDecoderOption) {
if option != nil {
if option.Metadata != nil {
s.Metadata = option.Metadata
}
if option.SubType != "" {
s.SubType = option.SubType
}
if option.Spec != 0 {
s.Spec = option.Spec
}
if option.Module != "" {
s.Module = option.Module
}
if option.TypeName != "" {
s.TypeName = option.TypeName
}
}
s.Data = data
s.RawValue = ""
s.Value = nil
if s.TypeMapping == nil && s.TypeString != "" {
s.buildStruct()
}
}
func (s *ScaleDecoder) Process() {}
func (s *ScaleDecoder) Encode(interface{}) string { return "" }
func (s *ScaleDecoder) NextBytes(length int) []byte {
data := s.Data.GetNextBytes(length)
s.RawValue += utiles.BytesToHex(data)
return data
}
func (s *ScaleDecoder) GetNextU8() int {
b := s.NextBytes(1)
if len(b) > 0 {
return int(b[0])
}
return 0
}
func (s *ScaleDecoder) getNextBool() bool {
data := s.NextBytes(1)
return utiles.BytesToHex(data) == "01"
}
func (s *ScaleDecoder) reset() {
s.Data.Data = []byte{}
s.Data.Offset = 0
}
func (s *ScaleDecoder) buildStruct() {
if s.TypeString != "" && string(s.TypeString[0]) == "(" && s.TypeString[len(s.TypeString)-1:] == ")" {
var names, types []string
reg := regexp.MustCompile(`[\<\(](.*?)[\>\)]`)
typeString := s.TypeString[1 : len(s.TypeString)-1]
typeParts := reg.FindAllString(typeString, -1)
for _, part := range typeParts {
typeString = strings.ReplaceAll(typeString, part, strings.ReplaceAll(part, ",", "#"))
}
for k, v := range strings.Split(typeString, ",") {
v = strings.TrimSpace(v)
if v == "" {
continue
}
types = append(types, strings.ReplaceAll(strings.TrimSpace(v), "#", ","))
names = append(names, fmt.Sprintf("col%d", k+1))
}
s.TypeMapping = &TypeMapping{Names: names, Types: types}
}
}
func (s *ScaleDecoder) ProcessAndUpdateData(typeString string) interface{} {
r := RuntimeType{Module: s.Module}
if TypeRegistry == nil {
r.Reg()
}
class, value, subType := r.DecoderClass(typeString, s.Spec)
if class == nil {
panic(fmt.Sprintf("Not found decoder class %s", typeString))
}
offsetStart := s.Data.Offset
// init
method, exist := class.MethodByName("Init")
if !exist {
panic(fmt.Sprintf("%s not implement init function", typeString))
}
option := ScaleDecoderOption{SubType: subType, Spec: s.Spec, Metadata: s.Metadata, Module: s.Module, TypeName: typeString}
method.Func.Call([]reflect.Value{value, reflect.ValueOf(s.Data), reflect.ValueOf(&option)})
// process
value.MethodByName("Process").Call(nil)
elementData := value.Elem().FieldByName("Data").Interface().(ScaleBytes)
s.Data.Offset = elementData.Offset
s.Data.Data = elementData.Data
s.RawValue = utiles.BytesToHex(s.Data.Data[offsetStart:s.Data.Offset])
return value.Elem().FieldByName("Value").Interface()
}
func Encode(typeString string, data interface{}) string {
r := RuntimeType{}
if TypeRegistry == nil {
r.Reg()
}
class, value, _ := r.DecoderClass(typeString, -1)
if class == nil {
panic(fmt.Sprintf("Not found decoder class %s", typeString))
}
out := value.MethodByName("Encode").Call([]reflect.Value{reflect.ValueOf(data)})
return out[0].String()
}