-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_index.go
258 lines (217 loc) · 6.19 KB
/
writer_index.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
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
// Copyright (C) 2023 by Posit Software, PBC
package rsf
import (
"bytes"
"fmt"
"io"
"reflect"
)
/*
When writing a struct at position zero, we first write an index that
describes the struct fields in the object.
Format:
[header size]
[field 1 name size]
[field 1 name]
[field 1 type]
[field n name size]
[field n name]
[field n type]
Example:
0x48, 0x0, 0x0, 0x0, // 72 bytes full header size
0x7, 0x0, 0x0, 0x0, // 7 bytes
0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79, // "company"
0x1, 0x0, 0x0, 0x0, // FieldTypeVarStr
0x7, 0x0, 0x0, 0x0, // 7 bytes
0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79, // "myfloat"
0x6, 0x0, 0x0, 0x0, // FieldTypeFloat
0x5, 0x0, 0x0, 0x0, // 5 bytes
0x72, 0x65, 0x61, 0x64, 0x79, // "ready"
0x2, 0x0, 0x0, 0x0, // FieldTypeFixedStr
0x8, 0x0, 0x0, 0x0 // 8 in size
0x4, 0x0, 0x0, 0x0, // 4 bytes
0x6c, 0x69, 0x73, 0x74, // "list"
0x3, 0x0, 0x0, 0x0, // FieldTypeArray
0x2, 0x0, 0x0, 0x0, // Array length is 2
// ... write all array struct fields ....
0x4, 0x0, 0x0, 0x0, // 4 bytes
0x6e, 0x61, 0x6d, 0x65, // "name"
0x1, 0x0, 0x0, 0x0, // FieldTypeVarStr
0x8, 0x0, 0x0, 0x0, // 8 bytes
0x76, 0x65, 0x72, 0x69, 0x66, 0x69, 0x65, 0x64, // "verified"
0x2, 0x0, 0x0, 0x0, // FieldTypeFixedStr
0x8, 0x0, 0x0, 0x0 // 8 in size
*/
const (
FieldTypeVarStr = 1
FieldTypeFixedStr = 2
FieldTypeBool = 3
FieldTypeArray = 4
FieldTypeFloat = 6
FieldTypeInt64 = 7
)
func (f *rsfWriter) writeIndexObject(v reflect.Type, t *tag, buf *bytes.Buffer) (int, error) {
switch v.Kind() {
case reflect.Array, reflect.Slice:
return f.writeIndexArray(v, t, buf)
case reflect.Struct:
sz, _, err := f.writeIndexStruct(v, t, buf)
return sz, err
case reflect.String:
return f.writeIndexString(t, buf)
case reflect.Bool:
return f.writeIndexFixed(t, FieldTypeBool, buf)
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
return f.writeIndexFixed(t, FieldTypeInt64, buf)
case reflect.Float32, reflect.Float64:
return f.writeIndexFixed(t, FieldTypeFloat, buf)
default:
return 0, fmt.Errorf("unknown field type %#v: %#v", v.Kind(), v)
}
}
func (f *rsfWriter) writeIndexStruct(v reflect.Type, tParent *tag, buf *bytes.Buffer) (int, int, error) {
var totalSz int
var count int
for i := 0; i < v.NumField(); i++ {
t := &tag{}
skip, err := getTagInfo(v, i, t, tParent, "")
if err != nil {
return 0, 0, err
}
if !skip {
var sz int
sz, err = f.writeIndexObject(v.Field(i).Type, t, buf)
if err != nil {
return 0, 0, err
}
totalSz += sz
count++
}
}
return totalSz, count, nil
}
func (f *rsfWriter) writeIndexArray(v reflect.Type, t *tag, buf *bytes.Buffer) (int, error) {
var totalSz int
sz, err := f.WriteStringField(0, t.name, buf)
if err != nil {
return 0, err
}
totalSz += sz
sz, err = f.WriteSizeField(0, FieldTypeArray, buf)
if err != nil {
return 0, err
}
totalSz += sz
el := v.Elem()
// For an indexed struct array, find the index size
if f.version > 1 {
if el.Kind() == reflect.Struct && t.index != "" {
sz, err = f.WriteBoolField(0, true, buf)
if err != nil {
return 0, err
}
totalSz += sz
// Calculate the index field size. This can be done simply by getting the tag
// info for each subfield.
for i := 0; i < el.NumField(); i++ {
subT := &tag{}
_, err = getTagInfo(el, i, subT, t, "")
if err != nil {
return 0, err
}
}
// Ensure that the indexed field was found.
if t.indexSz == 0 {
return 0, fmt.Errorf("could not calculate indexed field %s size for array %s", t.index, t.name)
}
// Write the index type field
sz, err = f.WriteSizeField(0, t.indexType, buf)
if err != nil {
return 0, err
}
totalSz += sz
// Write the index field size
sz, err = f.WriteSizeField(0, t.indexSz, buf)
if err != nil {
return 0, err
}
totalSz += sz
} else {
sz, err = f.WriteBoolField(0, false, buf)
if err != nil {
return 0, err
}
totalSz += sz
}
}
// For struct arrays, we may need to write additional info about the struct
var subfields int
subfieldsBuf := &bytes.Buffer{}
if el.Kind() == reflect.Struct {
// Write the subfields into a buffer and record the number of subfields found.
_, subfields, err = f.writeIndexStruct(el, t, subfieldsBuf)
if err != nil {
return 0, err
}
}
// Write the array type field
if f.version > 1 {
sz, err = f.WriteSizeField(0, int(el.Kind()), buf)
if err != nil {
return 0, err
}
totalSz += sz
}
// Record the number of subfields in the array
sz, err = f.WriteSizeField(0, subfields, buf)
if err != nil {
return 0, err
}
totalSz += sz
// For if subfields were found, copy the subfield buffer.
if subfields > 0 {
var szCopy int64
szCopy, err = io.Copy(buf, subfieldsBuf)
if err != nil {
return 0, err
}
totalSz += int(szCopy)
}
return totalSz, err
}
func (f *rsfWriter) writeIndexString(t *tag, buf *bytes.Buffer) (int, error) {
if t.fixed > 0 {
sz, err := f.writeIndexFixed(t, FieldTypeFixedStr, buf)
if err != nil {
return 0, err
}
sizeSz, err := f.WriteSizeField(0, t.fixed, buf)
return sz + sizeSz, err
}
var totalSz int
sz, err := f.WriteStringField(0, t.name, buf)
if err != nil {
return 0, err
}
totalSz += sz
sz, err = f.WriteSizeField(0, FieldTypeVarStr, buf)
if err != nil {
return 0, err
}
totalSz += sz
return totalSz, err
}
func (f *rsfWriter) writeIndexFixed(t *tag, fieldType int, buf *bytes.Buffer) (int, error) {
var totalSz int
sz, err := f.WriteStringField(0, t.name, buf)
if err != nil {
return 0, err
}
totalSz += sz
sz, err = f.WriteSizeField(0, fieldType, buf)
if err != nil {
return 0, err
}
totalSz += sz
return totalSz, err
}