forked from AmarnathCJD/gogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
182 lines (144 loc) · 4.07 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
// Copyright (c) 2023 RoseLoverX
package tl
import (
"bytes"
"fmt"
"reflect"
"github.com/pkg/errors"
)
func Marshal(v any) ([]byte, error) {
buf := bytes.NewBuffer(nil)
encoder := NewEncoder(buf)
encoder.encodeValue(reflect.ValueOf(v))
if err := encoder.CheckErr(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (c *Encoder) encodeValue(value reflect.Value) {
if m, ok := value.Interface().(Marshaler); ok {
if c.err != nil {
return
}
c.err = m.MarshalTL(c)
return
}
switch value.Type().Kind() { //nolint:exhaustive has default case
case reflect.Uint32:
c.PutUint(uint32(value.Uint()))
case reflect.Int32:
c.PutUint(uint32(value.Int()))
case reflect.Int64:
c.PutLong(value.Int())
case reflect.Float64:
c.PutDouble(value.Float())
case reflect.Bool:
c.PutBool(value.Bool())
case reflect.String:
c.PutString(value.String())
case reflect.Struct:
c.encodeStruct(value.Addr())
case reflect.Ptr, reflect.Interface:
if value.IsNil() {
c.err = errors.New("value can't be nil")
break
}
c.encodeValue(value.Elem())
case reflect.Slice:
if b, ok := value.Interface().([]byte); ok {
c.PutMessage(b)
break
}
c.encodeVector(sliceToInterfaceSlice(value.Interface())...)
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint64:
c.err = fmt.Errorf("int kind: %v (must be converted to int32, int64 or uint32 explicitly)", value.Kind())
case reflect.Float32, reflect.Complex64, reflect.Complex128:
c.err = fmt.Errorf("float kind: %s (must be converted to float64 explicitly)", value.Kind())
default:
c.err = fmt.Errorf("unsupported type: %v", value.Type())
}
}
// v must be pointer to struct
func (c *Encoder) encodeStruct(v reflect.Value) {
if c.err != nil {
return
}
o, ok := v.Interface().(Object)
if !ok {
c.err = errors.New(v.Type().String() + " doesn't implement tl.Object interface")
return
}
var hasFlagsField bool
var flag uint32
var flagIndex int
g, ok := v.Interface().(FlagIndexGetter)
if ok {
hasFlagsField = true
flagIndex = g.FlagIndex()
}
v = reflect.Indirect(v)
// what we checked and what we know about value:
// 1) it's not Marshaler (marshaler object already parsing in c.encodeValue())
// 2) implements tl.Object
// 3) definitely struct (we don't call encodeStruct(), only in c.encodeValue())
// 4) not nil (structs can't be nil, only pointers and interfaces)
c.PutCRC(o.CRC())
var tmpObjects = make([]reflect.Value, 0)
vtyp := v.Type()
for i := 0; i < v.NumField(); i++ {
// THIS PART is appending to object meta value, that actually don't writing in real encodeValue
if hasFlagsField && flagIndex == i {
tmpObjects = append(tmpObjects, reflect.ValueOf(0))
}
info, err := parseTag(vtyp.Field(i).Tag)
if err != nil {
c.err = errors.Wrapf(err, "parsing tag of field %v", vtyp.Field(i).Name)
return
}
if info == nil {
// если тега нет, то это обязательное поле, значит 100% записываем
tmpObjects = append(tmpObjects, v.Field(i))
continue
}
if info.ignore {
continue
}
if info.encodedInBitflag && vtyp.Field(i).Type.Kind() != reflect.Bool {
c.err = fmt.Errorf("field '%s': only bool values can be encoded in bitflag", vtyp.Field(i).Name)
return
}
fieldVal := v.Field(i)
if !fieldVal.IsZero() {
// тег есть, это 100% опциональное поле
flag |= 1 << info.index
if info.encodedInBitflag {
continue
}
tmpObjects = append(tmpObjects, v.Field(i))
continue
}
}
for i, elem := range tmpObjects {
// if you asking, wtf is here: continuing, cause we injected int value (native int, not int32), so we
// CAN skip this iter
if hasFlagsField && flagIndex == i {
c.PutUint(flag)
continue
}
c.encodeValue(elem)
if c.err != nil {
return
}
}
}
func (c *Encoder) encodeVector(slice ...any) {
c.PutCRC(CrcVector)
c.PutUint(uint32(len(slice)))
for i, item := range slice {
c.encodeValue(reflect.ValueOf(item))
if c.CheckErr() != nil {
c.err = errors.Wrapf(c.err, "[%v]", i)
}
}
}