-
Notifications
You must be signed in to change notification settings - Fork 14
/
marshalBuiltins.go
92 lines (84 loc) · 2.85 KB
/
marshalBuiltins.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
package obj
import (
"reflect"
. "github.com/polydawn/refmt/tok"
)
type ptrDerefDelegateMarshalMachine struct {
MarshalMachine
peelCount int
isNil bool
}
func (mach *ptrDerefDelegateMarshalMachine) Reset(slab *marshalSlab, rv reflect.Value, _ reflect.Type) error {
mach.isNil = false
for i := 0; i < mach.peelCount; i++ {
if rv.IsNil() {
mach.isNil = true
return nil
}
rv = rv.Elem()
}
return mach.MarshalMachine.Reset(slab, rv, rv.Type()) // REVIEW: we could have cached the peeled rt at mach conf time; worth it?
}
func (mach *ptrDerefDelegateMarshalMachine) Step(driver *Marshaller, slab *marshalSlab, tok *Token) (done bool, err error) {
if mach.isNil {
tok.Type = TNull
return true, nil
}
return mach.MarshalMachine.Step(driver, slab, tok)
}
type marshalMachinePrimitive struct {
kind reflect.Kind
rv reflect.Value
}
func (mach *marshalMachinePrimitive) Reset(_ *marshalSlab, rv reflect.Value, _ reflect.Type) error {
mach.rv = rv
return nil
}
func (mach *marshalMachinePrimitive) Step(_ *Marshaller, _ *marshalSlab, tok *Token) (done bool, err error) {
switch mach.kind {
case reflect.Bool:
tok.Type = TBool
tok.Bool = mach.rv.Bool()
return true, nil
case reflect.String:
tok.Type = TString
tok.Str = mach.rv.String()
return true, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
tok.Type = TInt
tok.Int = mach.rv.Int()
return true, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
tok.Type = TUint
tok.Uint = mach.rv.Uint()
return true, nil
case reflect.Float32, reflect.Float64:
tok.Type = TFloat64
tok.Float64 = mach.rv.Float()
return true, nil
case reflect.Slice: // implicitly bytes; no other slices are "primitive"
if mach.rv.IsNil() {
tok.Type = TNull
return true, nil
}
tok.Type = TBytes
tok.Bytes = mach.rv.Bytes()
return true, nil
case reflect.Array: // implicitly bytes; no other arrays are "primitive"
tok.Type = TBytes
// Unfortunately, there does not seem to be any efficient way to extract the contents of a byte array into a slice via reflect.
// Since the lengths are part of the type, it is almost understandable that the stdlib reflect package has a hard time expressing this;
// however, it drives me somewhat up the wall that they do not provide a case for arrays inside the `Value.Bytes` method, and panic.
// Attempting to `Value.Convert(Type)` from a fixed-length array to a slice of the same type is also rejected.
// Nor does `reflect.AppendSlice` accept an array kind as the second parameter; no, only slices there too.
// So... we iterate. If anyone knows a better way to do this, PRs extremely welcome.
n := mach.rv.Len()
tok.Bytes = make([]byte, n)
for i := 0; i < n; i++ {
tok.Bytes[i] = byte(mach.rv.Index(i).Uint())
}
return true, nil
default:
panic("unhandled")
}
}