forked from capnproto/go-capnp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal.go
582 lines (546 loc) · 14.7 KB
/
marshal.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// Package text supports marshaling Cap'n Proto messages as text based on a schema.
package text
import (
"bytes"
"fmt"
"io"
"math"
"strconv"
"zombiezen.com/go/capnproto2"
"zombiezen.com/go/capnproto2/internal/nodemap"
"zombiezen.com/go/capnproto2/internal/schema"
"zombiezen.com/go/capnproto2/internal/strquote"
"zombiezen.com/go/capnproto2/schemas"
)
// Marker strings.
const (
voidMarker = "void"
interfaceMarker = "<external capability>"
anyPointerMarker = "<opaque pointer>"
)
// Marshal returns the text representation of a struct.
func Marshal(typeID uint64, s capnp.Struct) (string, error) {
buf := new(bytes.Buffer)
if err := NewEncoder(buf).Encode(typeID, s); err != nil {
return "", err
}
return buf.String(), nil
}
// MarshalList returns the text representation of a struct list.
func MarshalList(typeID uint64, l capnp.List) (string, error) {
buf := new(bytes.Buffer)
if err := NewEncoder(buf).EncodeList(typeID, l); err != nil {
return "", err
}
return buf.String(), nil
}
// An Encoder writes the text format of Cap'n Proto messages to an output stream.
type Encoder struct {
w indentWriter
tmp []byte
nodes nodemap.Map
}
// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: indentWriter{w: w}}
}
// UseRegistry changes the registry that the encoder consults for
// schemas from the default registry.
func (enc *Encoder) UseRegistry(reg *schemas.Registry) {
enc.nodes.UseRegistry(reg)
}
// SetIndent sets string to indent each level with.
// An empty string disables indentation.
func (enc *Encoder) SetIndent(indent string) {
enc.w.indentPerLevel = indent
}
// Encode writes the text representation of s to the stream.
func (enc *Encoder) Encode(typeID uint64, s capnp.Struct) error {
if enc.w.err != nil {
return enc.w.err
}
err := enc.marshalStruct(typeID, s)
if err != nil {
return err
}
return enc.w.err
}
// EncodeList writes the text representation of struct list l to the stream.
func (enc *Encoder) EncodeList(typeID uint64, l capnp.List) error {
_, seg, _ := capnp.NewMessage(capnp.SingleSegment(nil))
typ, _ := schema.NewRootType(seg)
typ.SetStructType()
typ.StructType().SetTypeId(typeID)
return enc.marshalList(typ, l)
}
func (enc *Encoder) marshalBool(v bool) {
if v {
enc.w.WriteString("true")
} else {
enc.w.WriteString("false")
}
}
func (enc *Encoder) marshalInt(i int64) {
enc.tmp = strconv.AppendInt(enc.tmp[:0], i, 10)
enc.w.Write(enc.tmp)
}
func (enc *Encoder) marshalUint(i uint64) {
enc.tmp = strconv.AppendUint(enc.tmp[:0], i, 10)
enc.w.Write(enc.tmp)
}
func (enc *Encoder) marshalFloat32(f float32) {
enc.tmp = strconv.AppendFloat(enc.tmp[:0], float64(f), 'g', -1, 32)
enc.w.Write(enc.tmp)
}
func (enc *Encoder) marshalFloat64(f float64) {
enc.tmp = strconv.AppendFloat(enc.tmp[:0], f, 'g', -1, 64)
enc.w.Write(enc.tmp)
}
func (enc *Encoder) marshalText(t []byte) {
enc.tmp = strquote.Append(enc.tmp[:0], t)
enc.w.Write(enc.tmp)
}
func needsEscape(b byte) bool {
return b < 0x20 || b >= 0x7f
}
func hexDigit(b byte) byte {
const digits = "0123456789abcdef"
return digits[b]
}
func (enc *Encoder) marshalStruct(typeID uint64, s capnp.Struct) error {
n, err := enc.nodes.Find(typeID)
if err != nil {
return err
}
if !n.IsValid() || n.Which() != schema.Node_Which_structNode {
return fmt.Errorf("cannot find struct type %#x", typeID)
}
var discriminant uint16
if n.StructNode().DiscriminantCount() > 0 {
discriminant = s.Uint16(capnp.DataOffset(n.StructNode().DiscriminantOffset() * 2))
}
fields := codeOrderFields(n.StructNode())
if len(fields) == 0 {
enc.w.WriteString("()")
return nil
}
enc.w.WriteByte('(')
enc.w.Indent()
enc.w.NewLine()
first := true
for _, f := range fields {
if !(f.Which() == schema.Field_Which_slot || f.Which() == schema.Field_Which_group) {
continue
}
if dv := f.DiscriminantValue(); !(dv == schema.Field_noDiscriminant || dv == discriminant) {
continue
}
if !first {
enc.w.WriteByte(',')
enc.w.NewLineOrSpace()
}
first = false
name, err := f.NameBytes()
if err != nil {
return err
}
enc.w.Write(name)
enc.w.WriteString(" = ")
switch f.Which() {
case schema.Field_Which_slot:
if err := enc.marshalFieldValue(s, f); err != nil {
return err
}
case schema.Field_Which_group:
if err := enc.marshalStruct(f.Group().TypeId(), s); err != nil {
return err
}
}
}
enc.w.NewLine()
enc.w.Unindent()
enc.w.WriteByte(')')
return nil
}
func (enc *Encoder) marshalFieldValue(s capnp.Struct, f schema.Field) error {
typ, err := f.Slot().Type()
if err != nil {
return err
}
dv, err := f.Slot().DefaultValue()
if err != nil {
return err
}
if dv.IsValid() && int(typ.Which()) != int(dv.Which()) {
name, _ := f.Name()
return fmt.Errorf("marshal field %s: default value is a %v, want %v", name, dv.Which(), typ.Which())
}
switch typ.Which() {
case schema.Type_Which_void:
enc.w.WriteString(voidMarker)
case schema.Type_Which_bool:
v := s.Bit(capnp.BitOffset(f.Slot().Offset()))
d := dv.Bool()
enc.marshalBool(!d && v || d && !v)
case schema.Type_Which_int8:
v := s.Uint8(capnp.DataOffset(f.Slot().Offset()))
d := uint8(dv.Int8())
enc.marshalInt(int64(int8(v ^ d)))
case schema.Type_Which_int16:
v := s.Uint16(capnp.DataOffset(f.Slot().Offset() * 2))
d := uint16(dv.Int16())
enc.marshalInt(int64(int16(v ^ d)))
case schema.Type_Which_int32:
v := s.Uint32(capnp.DataOffset(f.Slot().Offset() * 4))
d := uint32(dv.Int32())
enc.marshalInt(int64(int32(v ^ d)))
case schema.Type_Which_int64:
v := s.Uint64(capnp.DataOffset(f.Slot().Offset() * 8))
d := uint64(dv.Int64())
enc.marshalInt(int64(v ^ d))
case schema.Type_Which_uint8:
v := s.Uint8(capnp.DataOffset(f.Slot().Offset()))
d := dv.Uint8()
enc.marshalUint(uint64(v ^ d))
case schema.Type_Which_uint16:
v := s.Uint16(capnp.DataOffset(f.Slot().Offset() * 2))
d := dv.Uint16()
enc.marshalUint(uint64(v ^ d))
case schema.Type_Which_uint32:
v := s.Uint32(capnp.DataOffset(f.Slot().Offset() * 4))
d := dv.Uint32()
enc.marshalUint(uint64(v ^ d))
case schema.Type_Which_uint64:
v := s.Uint64(capnp.DataOffset(f.Slot().Offset() * 8))
d := dv.Uint64()
enc.marshalUint(v ^ d)
case schema.Type_Which_float32:
v := s.Uint32(capnp.DataOffset(f.Slot().Offset() * 4))
d := math.Float32bits(dv.Float32())
enc.marshalFloat32(math.Float32frombits(v ^ d))
case schema.Type_Which_float64:
v := s.Uint64(capnp.DataOffset(f.Slot().Offset() * 8))
d := math.Float64bits(dv.Float64())
enc.marshalFloat64(math.Float64frombits(v ^ d))
case schema.Type_Which_structType:
p, err := s.Ptr(uint16(f.Slot().Offset()))
if err != nil {
return err
}
if !p.IsValid() {
p, _ = dv.StructValuePtr()
}
return enc.marshalStruct(typ.StructType().TypeId(), p.Struct())
case schema.Type_Which_data:
p, err := s.Ptr(uint16(f.Slot().Offset()))
if err != nil {
return err
}
if !p.IsValid() {
b, _ := dv.Data()
enc.marshalText(b)
return nil
}
enc.marshalText(p.Data())
case schema.Type_Which_text:
p, err := s.Ptr(uint16(f.Slot().Offset()))
if err != nil {
return err
}
if !p.IsValid() {
b, _ := dv.TextBytes()
enc.marshalText(b)
return nil
}
enc.marshalText(p.TextBytes())
case schema.Type_Which_list:
elem, err := typ.List().ElementType()
if err != nil {
return err
}
p, err := s.Ptr(uint16(f.Slot().Offset()))
if err != nil {
return err
}
if !p.IsValid() {
p, _ = dv.ListPtr()
}
return enc.marshalList(elem, p.List())
case schema.Type_Which_enum:
v := s.Uint16(capnp.DataOffset(f.Slot().Offset() * 2))
d := dv.Uint16()
return enc.marshalEnum(typ.Enum().TypeId(), v^d)
case schema.Type_Which_interface:
enc.w.WriteString(interfaceMarker)
case schema.Type_Which_anyPointer:
enc.w.WriteString(anyPointerMarker)
default:
return fmt.Errorf("unknown field type %v", typ.Which())
}
return nil
}
func codeOrderFields(s schema.Node_structNode) []schema.Field {
list, _ := s.Fields()
n := list.Len()
fields := make([]schema.Field, n)
for i := 0; i < n; i++ {
f := list.At(i)
fields[f.CodeOrder()] = f
}
return fields
}
func (enc *Encoder) marshalList(elem schema.Type, l capnp.List) error {
writeListItems := func(writeItem func(i int) error) error {
if l.Len() == 0 {
_, err := enc.w.WriteString("[]")
return err
}
enc.w.WriteByte('[')
enc.w.Indent()
enc.w.NewLine()
for i := 0; i < l.Len(); i++ {
err := writeItem(i)
if err != nil {
return err
}
if i == l.Len()-1 {
enc.w.NewLine()
} else {
enc.w.WriteByte(',')
enc.w.NewLineOrSpace()
}
}
enc.w.Unindent()
enc.w.WriteByte(']')
return nil
}
writeListItemsN := func(writeItem func(i int) (int, error)) error {
return writeListItems(func(i int) error {
_, err := writeItem(i)
return err
})
}
switch elem.Which() {
case schema.Type_Which_void:
return writeListItemsN(func(_ int) (int, error) {
return enc.w.WriteString("void")
})
case schema.Type_Which_bool:
p := capnp.BitList{List: l}
return writeListItemsN(func(i int) (int, error) {
if p.At(i) {
return enc.w.WriteString("true")
} else {
return enc.w.WriteString("false")
}
})
case schema.Type_Which_int8:
p := capnp.Int8List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatInt(int64(p.At(i)), 10))
})
case schema.Type_Which_int16:
p := capnp.Int16List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatInt(int64(p.At(i)), 10))
})
case schema.Type_Which_int32:
p := capnp.Int32List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatInt(int64(p.At(i)), 10))
})
case schema.Type_Which_int64:
p := capnp.Int64List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatInt(p.At(i), 10))
})
case schema.Type_Which_uint8:
p := capnp.UInt8List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatUint(uint64(p.At(i)), 10))
})
case schema.Type_Which_uint16:
p := capnp.UInt16List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatUint(uint64(p.At(i)), 10))
})
case schema.Type_Which_uint32:
p := capnp.UInt32List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatUint(uint64(p.At(i)), 10))
})
case schema.Type_Which_uint64:
p := capnp.UInt64List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatUint(p.At(i), 10))
})
case schema.Type_Which_float32:
p := capnp.Float32List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatFloat(float64(p.At(i)), 'g', -1, 32))
})
case schema.Type_Which_float64:
p := capnp.Float64List{List: l}
return writeListItemsN(func(i int) (int, error) {
return enc.w.WriteString(strconv.FormatFloat(p.At(i), 'g', -1, 64))
})
case schema.Type_Which_data:
p := capnp.DataList{List: l}
return writeListItemsN(func(i int) (int, error) {
s, err := p.At(i)
if err != nil {
return enc.w.WriteString("<error>")
}
buf := strquote.Append(nil, s)
return enc.w.Write(buf)
})
case schema.Type_Which_text:
p := capnp.TextList{List: l}
return writeListItemsN(func(i int) (int, error) {
s, err := p.BytesAt(i)
if err != nil {
return enc.w.WriteString("<error>")
}
buf := strquote.Append(nil, s)
return enc.w.Write(buf)
})
case schema.Type_Which_structType:
return writeListItems(func(i int) error {
return enc.marshalStruct(elem.StructType().TypeId(), l.Struct(i))
})
case schema.Type_Which_list:
ee, err := elem.List().ElementType()
if err != nil {
return err
}
return writeListItems(func(i int) error {
p, err := capnp.PointerList{List: l}.PtrAt(i)
if err != nil {
return err
}
return enc.marshalList(ee, p.List())
})
case schema.Type_Which_enum:
il := capnp.UInt16List{List: l}
typ := elem.Enum().TypeId()
// TODO(light): only search for node once
return writeListItems(func(i int) error {
return enc.marshalEnum(typ, il.At(i))
})
case schema.Type_Which_interface:
return writeListItemsN(func(_ int) (int, error) {
return enc.w.WriteString(interfaceMarker)
})
case schema.Type_Which_anyPointer:
return writeListItemsN(func(_ int) (int, error) {
return enc.w.WriteString(anyPointerMarker)
})
default:
return fmt.Errorf("unknown list type %v", elem.Which())
}
}
func (enc *Encoder) marshalEnum(typ uint64, val uint16) error {
n, err := enc.nodes.Find(typ)
if err != nil {
return err
}
if n.Which() != schema.Node_Which_enum {
return fmt.Errorf("marshaling enum of type @%#x: type is not an enum", typ)
}
enums, err := n.Enum().Enumerants()
if err != nil {
return err
}
if int(val) >= enums.Len() {
enc.marshalUint(uint64(val))
return nil
}
name, err := enums.At(int(val)).NameBytes()
if err != nil {
return err
}
enc.w.Write(name)
return nil
}
// indentWriter is helper for writing indented text
type indentWriter struct {
w io.Writer
err error
// indentPerLevel is a string to prepend to a line for every level of indentation.
indentPerLevel string
// current indent level
currentIndent int
// hasLineContent is true when we have written something on the current line.
hasLineContent bool
}
func (iw *indentWriter) beforeWrite() {
if iw.err != nil {
return
}
if len(iw.indentPerLevel) > 0 && !iw.hasLineContent {
iw.hasLineContent = true
for i := 0; i < iw.currentIndent; i++ {
_, err := iw.w.Write([]byte(iw.indentPerLevel))
if err != nil {
iw.err = err
return
}
}
}
}
func (iw *indentWriter) Write(p []byte) (int, error) {
iw.beforeWrite()
if iw.err != nil {
return 0, iw.err
}
var n int
n, iw.err = iw.w.Write(p)
return n, iw.err
}
func (iw *indentWriter) WriteString(s string) (int, error) {
iw.beforeWrite()
if iw.err != nil {
return 0, iw.err
}
var n int
n, iw.err = io.WriteString(iw.w, s)
return n, iw.err
}
func (iw *indentWriter) WriteByte(b byte) error {
iw.beforeWrite()
if iw.err != nil {
return iw.err
}
if bw, ok := iw.w.(io.ByteWriter); ok {
iw.err = bw.WriteByte(b)
} else {
_, iw.err = iw.w.Write([]byte{b})
}
return iw.err
}
func (iw *indentWriter) Indent() {
iw.currentIndent++
}
func (iw *indentWriter) Unindent() {
iw.currentIndent--
}
func (iw *indentWriter) NewLine() {
if len(iw.indentPerLevel) > 0 && iw.hasLineContent {
if iw.err != nil {
return
}
if bw, ok := iw.w.(io.ByteWriter); ok {
iw.err = bw.WriteByte('\n')
} else {
_, iw.err = iw.w.Write([]byte{'\n'})
}
iw.hasLineContent = false
}
}
func (iw *indentWriter) NewLineOrSpace() {
if len(iw.indentPerLevel) > 0 {
iw.NewLine()
} else {
iw.WriteByte(' ')
}
}