-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapencoder.go
More file actions
287 lines (230 loc) · 6.95 KB
/
mapencoder.go
File metadata and controls
287 lines (230 loc) · 6.95 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package glint
import (
"reflect"
"time"
"unsafe"
)
type mapEncoder struct {
instruction func(t unsafe.Pointer, w *Buffer) // the function we'll run to encode our date
tt reflect.Type // the type of data we're encoding
offset uintptr // used in the pointer arrhythmic to traverse through the arrays at runtime
schema *Buffer // if our data requires us to define more schema data it will be written here
}
func newMapEncoderUsingTagWithSchemaAndOpts(t any, usingTagName string, sc *Buffer, opts tagOptions) *mapEncoder {
m := &mapEncoder{}
m.schema = sc
m.tt = reflect.TypeOf(t)
m.offset = m.tt.Elem().Size()
// used for iterating through the slice elements
/// yeah we could save a few lines of code here if we went and added the 'AppendString' bit
/// below into a map based on the type, but then we wouldn't get any inlining from the compiler
/// ... so we're doing it the hard way.
key := m.tt.Key()
value := m.tt.Elem()
keyType := ReflectKindToWireType(m.tt.Key())
valueType := ReflectKindToWireType(m.tt.Elem())
m.schema.AppendUint(uint(keyType))
m.schema.AppendUint(uint(valueType))
switch {
case key.Kind() == reflect.String && value.Kind() == reflect.String:
m.instruction = func(t unsafe.Pointer, w *Buffer) {
m := *(*map[string]string)(t)
if *(*unsafe.Pointer)(t) == unsafe.Pointer(nil) {
w.AppendUint(0) // zero length
return
}
w.AppendUint(uint(len(m))) // length
for k, v := range m {
w.AppendString(k)
w.AppendString(v)
}
}
case key.Kind() == reflect.String && value.Kind() == reflect.Int:
m.instruction = func(t unsafe.Pointer, w *Buffer) {
m := *(*map[string]int)(t)
if *(*unsafe.Pointer)(t) == unsafe.Pointer(nil) {
w.AppendUint(0) // zero length
return
}
w.AppendUint(uint(len(m))) // length
for k, v := range m {
w.AppendString(k)
w.AppendInt(v)
}
}
default:
k := reflectKindToAppender(key, usingTagName, opts)
if k.subenc != nil {
m.schema.Bytes = append(m.schema.Bytes, k.subenc.Schema().Bytes...)
k.subenc.ClearSchema()
}
v := reflectKindToAppender(value, usingTagName, opts)
if v.subenc != nil {
m.schema.Bytes = append(m.schema.Bytes, v.subenc.Schema().Bytes...)
v.subenc.ClearSchema()
}
m.instruction = func(t unsafe.Pointer, w *Buffer) {
m := reflect.NewAt(m.tt, t).Elem()
if *(*unsafe.Pointer)(t) == unsafe.Pointer(nil) {
w.AppendUint(0) // zero length
return
}
w.AppendUint(uint(m.Len())) // length
iter := m.MapRange()
for iter.Next() {
key := iter.Key()
value := iter.Value()
var keyPtr, valuePtr unsafe.Pointer
if key.CanAddr() {
keyPtr = unsafe.Pointer(key.Addr().Pointer())
} else {
// Handle the case when key cannot be addressed
tempVal := reflect.New(key.Type()).Elem()
tempVal.Set(key)
keyPtr = unsafe.Pointer(tempVal.Addr().Pointer())
}
if value.CanAddr() {
valuePtr = unsafe.Pointer(value.Addr().Pointer())
} else {
// Handle the case when value cannot be addressed
tempVal := reflect.New(value.Type()).Elem()
tempVal.Set(value)
valuePtr = unsafe.Pointer(tempVal.Addr().Pointer())
}
k.fun(keyPtr, w)
v.fun(valuePtr, w)
}
}
}
return m
}
type appender struct {
fun func(unsafe.Pointer, *Buffer)
pointer bool
subenc encoder
}
// reflectKindToAppender returns a function that can be used to append the data
func reflectKindToAppender(k reflect.Type, usingTagName string, opts tagOptions) appender {
pointerWrap := false
var fun func(unsafe.Pointer, *Buffer)
var sub encoder
kind := k.Kind()
if kind == reflect.Pointer {
pointerWrap = true
kind = k.Elem().Kind()
}
switch kind {
case reflect.Uint8:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendUint8(*(*uint8)(p))
}
case reflect.Uint16:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendUint16(*(*uint16)(p))
}
case reflect.Uint32:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendUint32(*(*uint32)(p))
}
case reflect.Uint64:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendUint64(*(*uint64)(p))
}
case reflect.Uint:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendUint(*(*uint)(p))
}
case reflect.Int8:
// only needs to be defined if we're a pointer field due to the fast path in marshal
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendInt8(*(*int8)(p))
}
case reflect.Int16:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendInt16(*(*int16)(p))
}
case reflect.Int32:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendInt32(*(*int32)(p))
}
case reflect.Int64:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendInt64(*(*int64)(p))
}
case reflect.Float32:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendFloat32(*(*float32)(p))
}
case reflect.Float64:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendFloat64(*(*float64)(p))
}
case reflect.Bool:
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendBool(*(*bool)(p))
}
case reflect.Int:
// doesn't need to be defined unless its a pointer field due to the fast path in marshal
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendInt(*(*int)(p))
}
case reflect.String:
// this doesn't need to be defined unless we know its going to be a pointer, due to the fast path in marshal
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendString(*(*string)(p))
}
case reflect.Map:
mpEnc := newMapEncoderUsingTagWithSchemaAndOpts(reflect.New(k).Elem().Interface(), usingTagName, &Buffer{}, opts)
fun = func(p unsafe.Pointer, b *Buffer) {
var em = p
mpEnc.Marshal(em, b)
}
sub = mpEnc
case reflect.Slice:
// create a slice encoder to handle the slice type then hand off to it in the fun
slEnc := newSliceEncoderUsingTagWithSchemaAndOpts(reflect.New(k).Elem().Interface(), usingTagName, &Buffer{}, opts)
fun = func(p unsafe.Pointer, b *Buffer) {
var em = p
slEnc.Marshal(em, b)
}
sub = slEnc
case reflect.Struct:
// check first if we're a time field because we have a bespoke method for encoding time
if k == timeType {
fun = func(p unsafe.Pointer, b *Buffer) {
b.AppendTime(*(*time.Time)(p))
}
break
}
// create a new encoder to handle the sub-type and hand off to it in the fun
var inf any
if pointerWrap {
inf = reflect.New(k.Elem()).Elem().Interface()
} else {
inf = reflect.New(k).Elem().Interface()
}
se := newEncoderUsingTag(inf, usingTagName)
fun = func(p unsafe.Pointer, b *Buffer) {
var em any = p
se.Marshal(em, b)
}
sub = se
}
if fun == nil {
panic("invalid type passed to reflectKindToAppender")
}
if pointerWrap {
fun = derefAppend(fun)
}
return appender{fun: fun, pointer: pointerWrap, subenc: sub}
}
func (m *mapEncoder) Marshal(v any, b *Buffer) {
p := unsafe.Pointer(reflect.ValueOf(v).Pointer())
m.instruction(p, b)
}
func (m *mapEncoder) Schema() *Buffer {
return m.schema
}
func (m *mapEncoder) ClearSchema() {
m.schema.Reset()
}