-
Notifications
You must be signed in to change notification settings - Fork 2
/
sequence.go
195 lines (166 loc) · 4.03 KB
/
sequence.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package goscale
/*
Ref: https://spec.polkadot.network/#defn-scale-list
SCALE Sequence type translates to Go's slice and string types.
SCALE FixedSequence type translates to Go's array type.
*/
import (
"bytes"
)
type Sequence[T Encodable] []T
func (seq Sequence[T]) Encode(buffer *bytes.Buffer) error {
err := ToCompact(len(seq)).Encode(buffer)
if err != nil {
return err
}
for _, v := range seq {
//if reflect.TypeOf(v).Kind() == reflect.Struct {
// EncodeTuple(v, buffer)
//} else {
err := v.Encode(buffer)
if err != nil {
return err
}
//}
}
return nil
}
func (seq Sequence[T]) Bytes() []byte {
return EncodedBytes(seq)
}
func DecodeSequence[T Encodable](buffer *bytes.Buffer) (Sequence[T], error) {
size, err := DecodeCompact[U128](buffer)
if err != nil {
return Sequence[T]{}, err
}
v := size.ToBigInt()
values := make([]T, v.Int64())
for i := 0; i < len(values); i++ {
t, err := decodeByType(*new(T), buffer)
if err != nil {
return Sequence[T]{}, err
}
values[i] = t.(T)
}
return values, nil
}
func DecodeSequenceWith[T Encodable](buffer *bytes.Buffer, decodeFunc func(buffer *bytes.Buffer) (T, error)) (Sequence[T], error) {
size, err := DecodeCompact[U128](buffer)
if err != nil {
return Sequence[T]{}, err
}
v := size.ToBigInt()
values := make([]T, v.Int64())
for i := 0; i < len(values); i++ {
dec, err := decodeFunc(buffer)
if err != nil {
return Sequence[T]{}, err
}
values[i] = dec
}
return values, nil
}
func DecodeSliceU8(buffer *bytes.Buffer) ([]U8, error) {
sequence, err := DecodeSequence[U8](buffer)
if err != nil {
return make([]U8, 0), err
}
return sequence, nil
}
type FixedSequence[T Encodable] []T // TODO: https://github.com/LimeChain/goscale/issues/37
// Initializes with the specified size and provides size checks (at least at runtime)
func NewFixedSequence[T Encodable](size int, values ...T) FixedSequence[T] {
if len(values) != size {
panic("values size is out of the specified bound")
}
fseq := make(FixedSequence[T], size)
for i, v := range values {
fseq[i] = T(v)
}
return fseq
}
func (fseq FixedSequence[T]) Encode(buffer *bytes.Buffer) error {
for _, v := range fseq {
//if reflect.TypeOf(v).Kind() == reflect.Struct {
// EncodeTuple(v, buffer)
//} else {
err := v.Encode(buffer)
if err != nil {
return err
}
//}
}
return nil
}
func (fseq FixedSequence[T]) Bytes() []byte {
return EncodedBytes(fseq)
}
func DecodeFixedSequence[T Encodable](size int, buffer *bytes.Buffer) (FixedSequence[T], error) {
result := make([]T, size)
for i := 0; i < size; i++ {
t, err := decodeByType(*new(T), buffer)
if err != nil {
return FixedSequence[T]{}, err
}
result[i] = t.(T)
}
return result, nil
}
// additional helper type
type Str string
func (value Str) Encode(buffer *bytes.Buffer) error {
_, err := buffer.Write(value.Bytes())
return err
}
func (value Str) Bytes() []byte {
return Sequence[U8](StrToSliceU8(value)).Bytes()
}
func DecodeStr(buffer *bytes.Buffer) (Str, error) {
decodeSlice, err := DecodeSliceU8(buffer)
if err != nil {
return "", err
}
return SliceU8ToStr(decodeSlice), nil
}
func SliceU8ToStr(values []U8) Str {
result := make([]byte, len(values))
for i, v := range values {
result[i] = byte(v)
}
return Str(result)
}
func StrToSliceU8(s Str) []U8 {
result := make([]U8, len(s))
for i, v := range []byte(s) {
result[i] = U8(v)
}
return result
}
func SequenceU8ToBytes(bytes Sequence[U8]) []byte {
result := make([]byte, len(bytes))
for i, v := range bytes {
result[i] = byte(v)
}
return result
}
func FixedSequenceU8ToBytes(bytes FixedSequence[U8]) []byte {
result := make([]byte, len(bytes))
for i, v := range bytes {
result[i] = byte(v)
}
return result
}
func BytesToSequenceU8(bytes []byte) Sequence[U8] {
result := make(Sequence[U8], len(bytes))
for i, v := range bytes {
result[i] = U8(v)
}
return result
}
func BytesToFixedSequenceU8(bytes []byte) FixedSequence[U8] {
result := make(FixedSequence[U8], len(bytes))
for i, v := range bytes {
result[i] = U8(v)
}
return result
}