forked from tonkeeper/tongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
192 lines (176 loc) · 4.15 KB
/
encoder.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
package tlb
import (
"fmt"
"reflect"
"github.com/tonkeeper/tongo/boc"
)
type Encoder struct {
}
type MarshalerTLB interface {
MarshalTLB(c *boc.Cell, encoder *Encoder) error
}
type tagEncoder interface {
EncodeTag(c *boc.Cell, tag string) error
}
func Marshal(c *boc.Cell, o any) error {
encoder := Encoder{}
return encode(c, "", o, &encoder)
}
func (enc *Encoder) Marshal(c *boc.Cell, o any) error {
return encode(c, "", o, enc)
}
func isNil(o any) bool {
switch reflect.ValueOf(o).Kind() {
case reflect.Interface, reflect.Slice, reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer:
return reflect.ValueOf(o).IsNil()
}
return false
}
func encode(c *boc.Cell, tag string, o any, encoder *Encoder) error {
if m, ok := o.(tagEncoder); ok {
return m.EncodeTag(c, tag)
}
t, err := parseTag(tag)
if err != nil {
return err
}
tag = ""
switch {
case t.IsMaybeRef:
if isNil(o) {
err := c.WriteBit(false)
return err
}
if err := c.WriteBit(true); err != nil {
return err
}
c, err = c.NewRef()
if err != nil {
return err
}
case t.IsMaybe:
if isNil(o) {
err := c.WriteBit(false)
return err
}
if err := c.WriteBit(true); err != nil {
return err
}
case t.IsRef:
c, err = c.NewRef()
if err != nil {
return err
}
}
if m, ok := o.(MarshalerTLB); ok {
return m.MarshalTLB(c, encoder)
}
val := reflect.ValueOf(o)
switch val.Kind() {
case reflect.Uint8:
return c.WriteUint(val.Uint(), 8)
case reflect.Uint16:
return c.WriteUint(val.Uint(), 16)
case reflect.Uint32:
return c.WriteUint(val.Uint(), 32)
case reflect.Uint64:
return c.WriteUint(val.Uint(), 64)
case reflect.Int8:
return c.WriteInt(val.Int(), 8)
case reflect.Int16:
return c.WriteInt(val.Int(), 16)
case reflect.Int32:
return c.WriteInt(val.Int(), 32)
case reflect.Int64:
return c.WriteInt(val.Int(), 64)
case reflect.Bool:
err := c.WriteBit(val.Bool())
if err != nil {
return err
}
return nil
case reflect.Struct:
switch o.(type) {
case boc.Cell:
return encodeCell(c, o)
case boc.BitString:
return encodeBitString(c, o)
default:
return encodeStruct(c, o, encoder)
}
case reflect.Pointer:
if val.IsNil() && !t.IsOptional {
return fmt.Errorf("can't encode empty pointer %v if tlb scheme is not optional", val.Type())
}
return encode(c, tag, val.Elem().Interface(), encoder)
case reflect.Array:
if val.Type().Elem().Kind() != reflect.Uint8 {
return fmt.Errorf("encoding array of %v not supported", val.Type().Elem().Kind())
}
// TODO: optimize
b := make([]byte, 0, val.Len())
for i := 0; i < val.Len(); i++ {
b = append(b, uint8(val.Index(i).Uint()))
}
return c.WriteBytes(b)
case reflect.Slice:
if val.Type().Elem().Kind() != reflect.Uint8 {
return fmt.Errorf("encoding slice of %v not supported", val.Type().Elem().Kind())
}
return c.WriteBytes(val.Bytes())
default:
return fmt.Errorf("type %v not implemented", val.Kind())
}
}
func encodeStruct(c *boc.Cell, o any, encoder *Encoder) error {
val := reflect.ValueOf(o)
if _, ok := val.Type().FieldByName("SumType"); ok {
return encodeSumType(c, o, encoder)
} else {
return encodeBasicStruct(c, o, encoder)
}
}
func encodeBasicStruct(c *boc.Cell, o any, encoder *Encoder) error {
val := reflect.ValueOf(o)
for i := 0; i < val.NumField(); i++ {
tag := val.Type().Field(i).Tag.Get("tlb")
if err := encode(c, tag, val.Field(i).Interface(), encoder); err != nil {
return err
}
}
return nil
}
func encodeSumType(c *boc.Cell, o any, encoder *Encoder) error {
val := reflect.ValueOf(o)
name := val.FieldByName("SumType").String()
for i := 0; i < val.NumField(); i++ {
if val.Field(i).Type().Name() == "SumType" {
continue
}
tag := val.Type().Field(i).Tag.Get("tlbSumType")
if name != val.Type().Field(i).Name {
continue
}
err := encodeSumTag(c, tag)
if err != nil {
return err
}
err = encode(c, "", val.Field(i).Interface(), encoder)
if err != nil {
return err
}
break
}
return nil
}
func encodeCell(c *boc.Cell, o any) error {
*c = o.(boc.Cell)
return nil
}
func encodeBitString(c *boc.Cell, o any) error {
err := c.WriteBitString(o.(boc.BitString))
if err != nil {
return err
}
return nil
}