forked from mitchellh/go-mruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
511 lines (434 loc) · 13 KB
/
decode.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
package mruby
import (
"errors"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
)
// This is the tag to use with structures to have settings for mruby
const tagName = "mruby"
// Decode converts the Ruby value to a Go value.
//
// The Decode process may call Ruby code and may generate Ruby garbage,
// but it collects all of its own garbage. You don't need to GC around this.
//
// See the tests (decode_test.go) for detailed and specific examples of
// how this function decodes. Basic examples are also available here and
// in the README.
//
// For primitives, the decoding process is likely what you expect. For Ruby,
// this is booleans, strings, fixnums, and floats. These map directly to
// effectively equivalent Go types: bool, string, int, float64.
// Hash and Arrays can map directly to maps and slices in Go, and Decode
// will handle this as you expect.
//
// The only remaining data type in Go is a struct. A struct in Go can map
// to any object in Ruby. If the data in Ruby is a hash, then the struct keys
// will map directly to the hash keys. If the data in Ruby is an object, then
// one of two things will be done. First: if the object responds to the
// `to_gomruby` function, then this will be called and the resulting value
// is expected to be a Hash and will be used to decode into the struct. If
// the object does NOT respond to that function, then any struct fields will
// invoke the corresponding Ruby method to attain the value.
//
// Note that with structs you can use the `mruby` tag to specify the
// Hash key or method name to call. Example:
//
// type Foo struct {
// Field string `mruby:"read_field"`
// }
//
func Decode(out interface{}, v *MrbValue) error {
// The out parameter must be a pointer since we must be
// able to write to it.
val := reflect.ValueOf(out)
if val.Kind() != reflect.Ptr {
return errors.New("result must be a pointer")
}
var d decoder
return d.decode("root", v, val.Elem())
}
type decoder struct {
stack []reflect.Kind
}
type decodeStructGetter func(string) (*MrbValue, error)
func (d *decoder) decode(name string, v *MrbValue, result reflect.Value) error {
k := result
// If we have an interface with a valid value, we use that
// for the check.
if result.Kind() == reflect.Interface {
elem := result.Elem()
if elem.IsValid() {
k = elem
}
}
// Push current onto stack unless it is an interface.
if k.Kind() != reflect.Interface {
d.stack = append(d.stack, k.Kind())
// Schedule a pop
defer func() {
d.stack = d.stack[:len(d.stack)-1]
}()
}
switch k.Kind() {
case reflect.Bool:
return d.decodeBool(name, v, result)
case reflect.Float64:
return d.decodeFloat(name, v, result)
case reflect.Int:
return d.decodeInt(name, v, result)
case reflect.Interface:
// When we see an interface, we make our own thing
return d.decodeInterface(name, v, result)
case reflect.Map:
return d.decodeMap(name, v, result)
case reflect.Ptr:
return d.decodePtr(name, v, result)
case reflect.Slice:
return d.decodeSlice(name, v, result)
case reflect.String:
return d.decodeString(name, v, result)
case reflect.Struct:
return d.decodeStruct(name, v, result)
default:
}
return fmt.Errorf(
"%s: unknown kind to decode into: %s", name, k.Kind())
}
func (d *decoder) decodeBool(name string, v *MrbValue, result reflect.Value) error {
switch t := v.Type(); t {
case TypeFalse:
result.Set(reflect.ValueOf(false))
case TypeTrue:
result.Set(reflect.ValueOf(true))
default:
return fmt.Errorf("%s: unknown type %v", name, t)
}
return nil
}
func (d *decoder) decodeFloat(name string, v *MrbValue, result reflect.Value) error {
switch t := v.Type(); t {
case TypeFloat:
result.Set(reflect.ValueOf(v.Float()))
default:
return fmt.Errorf("%s: unknown type %v", name, t)
}
return nil
}
func (d *decoder) decodeInt(name string, v *MrbValue, result reflect.Value) error {
switch t := v.Type(); t {
case TypeFixnum:
result.Set(reflect.ValueOf(v.Fixnum()))
case TypeString:
v, err := strconv.ParseInt(v.String(), 0, 0)
if err != nil {
return err
}
result.SetInt(int64(v))
default:
return fmt.Errorf("%s: unknown type %v", name, t)
}
return nil
}
func (d *decoder) decodeInterface(name string, v *MrbValue, result reflect.Value) error {
var set reflect.Value
redecode := true
switch t := v.Type(); t {
case TypeHash:
var temp map[string]interface{}
tempVal := reflect.ValueOf(temp)
result := reflect.MakeMap(
reflect.MapOf(
reflect.TypeOf(""),
tempVal.Type().Elem()))
set = result
case TypeArray:
var temp []interface{}
tempVal := reflect.ValueOf(temp)
result := reflect.MakeSlice(
reflect.SliceOf(tempVal.Type().Elem()), 0, 0)
set = result
case TypeFalse:
fallthrough
case TypeTrue:
var result bool
set = reflect.Indirect(reflect.New(reflect.TypeOf(result)))
case TypeFixnum:
var result int
set = reflect.Indirect(reflect.New(reflect.TypeOf(result)))
case TypeFloat:
var result float64
set = reflect.Indirect(reflect.New(reflect.TypeOf(result)))
case TypeString:
set = reflect.Indirect(reflect.New(reflect.TypeOf("")))
default:
return fmt.Errorf(
"%s: cannot decode into interface: %v",
name, t)
}
// Set the result to what its supposed to be, then reset
// result so we don't reflect into this method anymore.
result.Set(set)
if redecode {
// Revisit the node so that we can use the newly instantiated
// thing and populate it.
if err := d.decode(name, v, result); err != nil {
return err
}
}
return nil
}
func (d *decoder) decodeMap(name string, v *MrbValue, result reflect.Value) error {
if v.Type() != TypeHash {
return fmt.Errorf("%s: not a hash type for map (%v)", name, v.Type())
}
// If we have an interface, then we can address the interface,
// but not the slice itself, so get the element but set the interface
set := result
if result.Kind() == reflect.Interface {
result = result.Elem()
}
resultType := result.Type()
resultElemType := resultType.Elem()
resultKeyType := resultType.Key()
if resultKeyType.Kind() != reflect.String {
return fmt.Errorf(
"%s: map must have string keys", name)
}
// Make a map if it is nil
resultMap := result
if result.IsNil() {
resultMap = reflect.MakeMap(
reflect.MapOf(resultKeyType, resultElemType))
}
// We're going to be allocating some garbage, so set the arena
// so it is cleared properly.
mrb := v.Mrb()
defer mrb.ArenaRestore(mrb.ArenaSave())
// Get the hash of the value
hash := v.Hash()
keysRaw, err := hash.Keys()
if err != nil {
return err
}
keys := keysRaw.Array()
for i := 0; i < keys.Len(); i++ {
// Get the key and value in Ruby. This should do no allocations.
rbKey, err := keys.Get(i)
if err != nil {
return err
}
rbVal, err := hash.Get(rbKey)
if err != nil {
return err
}
// Make the field name
fieldName := fmt.Sprintf("%s.<entry %d>", name, i)
// Decode the key into the key type
keyVal := reflect.Indirect(reflect.New(resultKeyType))
if err := d.decode(fieldName, rbKey, keyVal); err != nil {
return err
}
// Decode the value
val := reflect.Indirect(reflect.New(resultElemType))
if err := d.decode(fieldName, rbVal, val); err != nil {
return err
}
// Set the value on the map
resultMap.SetMapIndex(keyVal, val)
}
// Set the final map if we can
set.Set(resultMap)
return nil
}
func (d *decoder) decodePtr(name string, v *MrbValue, result reflect.Value) error {
// Create an element of the concrete (non pointer) type and decode
// into that. Then set the value of the pointer to this type.
resultType := result.Type()
resultElemType := resultType.Elem()
val := reflect.New(resultElemType)
if err := d.decode(name, v, reflect.Indirect(val)); err != nil {
return err
}
result.Set(val)
return nil
}
func (d *decoder) decodeSlice(name string, v *MrbValue, result reflect.Value) error {
// If we have an interface, then we can address the interface,
// but not the slice itself, so get the element but set the interface
set := result
if result.Kind() == reflect.Interface {
result = result.Elem()
}
// Create the slice if it isn't nil
resultType := result.Type()
resultElemType := resultType.Elem()
if result.IsNil() {
resultSliceType := reflect.SliceOf(resultElemType)
result = reflect.MakeSlice(
resultSliceType, 0, 0)
}
// Get the hash of the value
array := v.Array()
for i := 0; i < array.Len(); i++ {
// Get the key and value in Ruby. This should do no allocations.
rbVal, err := array.Get(i)
if err != nil {
return err
}
// Make the field name
fieldName := fmt.Sprintf("%s[%d]", name, i)
// Decode the value
val := reflect.Indirect(reflect.New(resultElemType))
if err := d.decode(fieldName, rbVal, val); err != nil {
return err
}
// Append it onto the slice
result = reflect.Append(result, val)
}
set.Set(result)
return nil
}
func (d *decoder) decodeString(name string, v *MrbValue, result reflect.Value) error {
switch t := v.Type(); t {
case TypeFixnum:
result.Set(reflect.ValueOf(
strconv.FormatInt(int64(v.Fixnum()), 10)).Convert(result.Type()))
case TypeString:
result.Set(reflect.ValueOf(v.String()).Convert(result.Type()))
default:
return fmt.Errorf("%s: unknown type to string: %v", name, t)
}
return nil
}
func (d *decoder) decodeStruct(name string, v *MrbValue, result reflect.Value) error {
var get decodeStructGetter
// We're going to be allocating some garbage, so set the arena
// so it is cleared properly.
mrb := v.Mrb()
defer mrb.ArenaRestore(mrb.ArenaSave())
// Depending on the type, we need to generate a getter
switch t := v.Type(); t {
case TypeHash:
get = decodeStructHashGetter(mrb, v.Hash())
case TypeObject:
get = decodeStructObjectMethods(mrb, v)
default:
return fmt.Errorf("%s: not an object type for struct (%v)", name, t)
}
// This slice will keep track of all the structs we'll be decoding.
// There can be more than one struct if there are embedded structs
// that are squashed.
structs := make([]reflect.Value, 1, 5)
structs[0] = result
// Compile the list of all the fields that we're going to be decoding
// from all the structs.
fields := make(map[*reflect.StructField]reflect.Value)
for len(structs) > 0 {
structVal := structs[0]
structs = structs[1:]
structType := structVal.Type()
for i := 0; i < structType.NumField(); i++ {
fieldType := structType.Field(i)
if fieldType.Anonymous {
fieldKind := fieldType.Type.Kind()
if fieldKind != reflect.Struct {
return fmt.Errorf(
"%s: unsupported type to struct: %s",
fieldType.Name, fieldKind)
}
// We have an embedded field. We "squash" the fields down
// if specified in the tag.
squash := false
tagParts := strings.Split(fieldType.Tag.Get(tagName), ",")
for _, tag := range tagParts[1:] {
if tag == "squash" {
squash = true
break
}
}
if squash {
structs = append(
structs, result.FieldByName(fieldType.Name))
continue
}
}
// Normal struct field, store it away
fields[&fieldType] = structVal.Field(i)
}
}
var (
decodedFields = make([]string, 0, len(fields))
decodedFieldsVal = []reflect.Value{}
usedKeys = make(map[string]struct{})
)
for fieldType, field := range fields {
if !field.IsValid() {
// This should never happen
panic("field is not valid")
}
// If we can't set the field, then it is unexported or something,
// and we just continue onwards.
if !field.CanSet() {
continue
}
fieldName := strings.ToLower(fieldType.Name)
tagValue := fieldType.Tag.Get(tagName)
tagParts := strings.SplitN(tagValue, ",", 2)
if len(tagParts) >= 2 {
switch tagParts[1] {
case "decodedFields":
decodedFieldsVal = append(decodedFieldsVal, field)
continue
}
}
if tagParts[0] != "" {
fieldName = tagParts[0]
}
// We move the arena for every value here so we don't
// generate too much intermediate garbage.
idx := mrb.ArenaSave()
// Get the Ruby string value
value, err := get(fieldName)
if err != nil {
mrb.ArenaRestore(idx)
return err
}
// Track the used key
usedKeys[fieldName] = struct{}{}
// Create the field name and decode. We range over the elements
// because we actually want the value.
fieldName = fmt.Sprintf("%s.%s", name, fieldName)
err = d.decode(fieldName, value, field)
mrb.ArenaRestore(idx)
if err != nil {
return err
}
decodedFields = append(decodedFields, fieldType.Name)
}
if len(decodedFieldsVal) > 0 {
// Sort it so that it is deterministic
sort.Strings(decodedFields)
for _, v := range decodedFieldsVal {
v.Set(reflect.ValueOf(decodedFields))
}
}
return nil
}
// decodeStructHashGetter is a decodeStructGetter that reads values from
// a hash.
func decodeStructHashGetter(mrb *Mrb, h *Hash) decodeStructGetter {
return func(key string) (*MrbValue, error) {
rbKey := mrb.StringValue(key)
return h.Get(rbKey)
}
}
// decodeStructObjectMethods is a decodeStructGetter that reads values from
// an object by calling methods.
func decodeStructObjectMethods(mrb *Mrb, v *MrbValue) decodeStructGetter {
return func(key string) (*MrbValue, error) {
return v.Call(key)
}
}