forked from streamingfast/binary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariant.go
241 lines (206 loc) · 7.05 KB
/
variant.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
package bin
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/tidwall/gjson"
)
//
/// Variant (emulates `fc::static_variant` type)
//
type Variant interface {
Assign(typeID uint, impl interface{})
Obtain() (typeID uint, impl interface{})
}
type VariantType struct {
Name string
Type interface{}
}
type VariantDefinition struct {
typeIDToType map[uint32]reflect.Type
typeIDToName map[uint32]string
typeNameToID map[string]uint32
typeIDEncoding TypeIDEncoding
}
type TypeIDEncoding uint32
const (
Uvarint32TypeIDEncoding TypeIDEncoding = iota
Uint32TypeIDEncoding
Uint8TypeIDEncoding
)
// NewVariantDefinition creates a variant definition based on the *ordered* provided types.
// It's the ordering that defines the binary variant value just like in native `nodeos` C++
// and in Smart Contract via the `std::variant` type. It's important to pass the entries
// in the right order!
//
// This variant definition can now be passed to functions of `BaseVariant` to implement
// marshal/unmarshaling functionalities for binary & JSON.
func NewVariantDefinition(typeIDEncoding TypeIDEncoding, types []VariantType) (out *VariantDefinition) {
if len(types) < 0 {
panic("it's not valid to create a variant definition without any types")
}
typeCount := len(types)
out = &VariantDefinition{
typeIDEncoding: typeIDEncoding,
typeIDToType: make(map[uint32]reflect.Type, typeCount),
typeIDToName: make(map[uint32]string, typeCount),
typeNameToID: make(map[string]uint32, typeCount),
}
for i, typeDef := range types {
typeID := uint32(i)
// FIXME: Check how the reflect.Type is used and cache all its usage in the definition.
// Right now, on each Unmarshal, we re-compute some expensive stuff that can be
// re-used like the `typeGo.Elem()` which is always the same. It would be preferable
// to have those already pre-defined here so we can actually speed up the
// Unmarshal code.
out.typeIDToType[typeID] = reflect.TypeOf(typeDef.Type)
out.typeIDToName[typeID] = typeDef.Name
out.typeNameToID[typeDef.Name] = typeID
}
return out
}
func (d *VariantDefinition) TypeID(name string) uint32 {
id, found := d.typeNameToID[name]
if !found {
knownNames := make([]string, len(d.typeNameToID))
i := 0
for name := range d.typeNameToID {
knownNames[i] = name
i++
}
panic(fmt.Errorf("trying to use an unknown type name %q, known names are %q", name, strings.Join(knownNames, ", ")))
}
return id
}
type VariantImplFactory = func() interface{}
type OnVariant = func(impl interface{}) error
type BaseVariant struct {
TypeID uint32
Impl interface{}
}
func (a *BaseVariant) Assign(typeID uint32, impl interface{}) {
a.TypeID = typeID
a.Impl = impl
}
func (a *BaseVariant) Obtain(def *VariantDefinition) (typeID uint32, typeName string, impl interface{}) {
return a.TypeID, def.typeIDToName[a.TypeID], a.Impl
}
func (a *BaseVariant) MarshalJSON(def *VariantDefinition) ([]byte, error) {
typeName, found := def.typeIDToName[a.TypeID]
if !found {
return nil, fmt.Errorf("type %d is not know by variant definition", a.TypeID)
}
return json.Marshal([]interface{}{typeName, a.Impl})
}
func (a *BaseVariant) UnmarshalJSON(data []byte, def *VariantDefinition) error {
typeResult := gjson.GetBytes(data, "0")
implResult := gjson.GetBytes(data, "1")
if !typeResult.Exists() || !implResult.Exists() {
return fmt.Errorf("invalid format, expected '[<type>, <impl>]' pair, got %q", string(data))
}
typeName := typeResult.String()
typeID, found := def.typeNameToID[typeName]
if !found {
return fmt.Errorf("type %q is not know by variant definition", typeName)
}
typeGo := def.typeIDToType[typeID]
if typeGo == nil {
return fmt.Errorf("no known type for %q", typeName)
}
a.TypeID = typeID
if typeGo.Kind() == reflect.Ptr {
a.Impl = reflect.New(typeGo.Elem()).Interface()
if err := json.Unmarshal([]byte(implResult.Raw), a.Impl); err != nil {
return err
}
} else {
// This is not the most optimal way of doing things for "value"
// types (over "pointer" types) as we always allocate a new pointer
// element, unmarshal it and then either keep the pointer type or turn
// it into a value type.
//
// However, in non-reflection based code, one would do like this and
// avoid an `new` memory allocation:
//
// ```
// name := eos.Name("")
// json.Unmarshal(data, &name)
// ```
//
// This would work without a problem. In reflection code however, I
// did not find how one can go from `reflect.Zero(typeGo)` (which is
// the equivalence of doing `name := eos.Name("")`) and take the
// pointer to it so it can be unmarshalled correctly.
//
// A played with various iteration, and nothing got it working. Maybe
// the next step would be to explore the `unsafe` package and obtain
// an unsafe pointer and play with it.
value := reflect.New(typeGo)
if err := json.Unmarshal([]byte(implResult.Raw), value.Interface()); err != nil {
return err
}
a.Impl = value.Elem().Interface()
}
return nil
}
func (a *BaseVariant) UnmarshalBinaryVariant(decoder *Decoder, def *VariantDefinition) (err error) {
var typeID uint32
switch def.typeIDEncoding {
case Uvarint32TypeIDEncoding:
typeID, err = decoder.ReadUvarint32()
if err != nil {
return fmt.Errorf("uvarint32: unable to read variant type id: %s", err)
}
case Uint32TypeIDEncoding:
typeID, err = decoder.ReadUint32(LE())
if err != nil {
return fmt.Errorf("uint32: unable to read variant type id: %s", err)
}
case Uint8TypeIDEncoding:
id, err := decoder.ReadUint8()
if err != nil {
return fmt.Errorf("uint8: unable to read variant type id: %s", err)
}
typeID = uint32(id)
}
a.TypeID = typeID
typeGo := def.typeIDToType[typeID]
if typeGo == nil {
return fmt.Errorf("no known type for type %d", typeID)
}
if typeGo.Kind() == reflect.Ptr {
a.Impl = reflect.New(typeGo.Elem()).Interface()
if err = decoder.Decode(a.Impl); err != nil {
return fmt.Errorf("unable to decode variant type %d: %s", typeID, err)
}
} else {
// This is not the most optimal way of doing things for "value"
// types (over "pointer" types) as we always allocate a new pointer
// element, unmarshal it and then either keep the pointer type or turn
// it into a value type.
//
// However, in non-reflection based code, one would do like this and
// avoid an `new` memory allocation:
//
// ```
// name := eos.Name("")
// json.Unmarshal(data, &name)
// ```
//
// This would work without a problem. In reflection code however, I
// did not find how one can go from `reflect.Zero(typeGo)` (which is
// the equivalence of doing `name := eos.Name("")`) and take the
// pointer to it so it can be unmarshalled correctly.
//
// A played with various iteration, and nothing got it working. Maybe
// the next step would be to explore the `unsafe` package and obtain
// an unsafe pointer and play with it.
value := reflect.New(typeGo)
if err = decoder.Decode(value.Interface()); err != nil {
return fmt.Errorf("unable to decode variant type %d: %s", typeID, err)
}
a.Impl = value.Elem().Interface()
}
return nil
}