Skip to content

Commit c0dd979

Browse files
committed
Fix Variant decoding, added a few more tests
1 parent ed8e922 commit c0dd979

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8-
### Changed
8+
### Added
9+
- Ability to decode nested `arrays`
910

11+
### Changed
12+
* BREAKING: The Decoding for `Variants` was not returning the decoded value type in the `json` representation. Now `Variants` would be decoded like `{"field":["uint32",100]}`
1013
* BREAKING: The serialization for `ExtendedAsset` was aligned with the `eos` codebase. Beforehand, it would serialize the field name `"Contract"` with a capital `C`, and the `Asset` field as `"asset"` where it should have been `"quantity"`.

abidecoder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,11 @@ func (a *ABI) read(binaryDecoder *Decoder, fieldName string, fieldType string, j
365365
abiDecoderLog.Debug("set field value", zap.String("name", fieldName), zap.Reflect("value", value))
366366
}
367367

368+
if variant != nil {
369+
// As a variant we need to include the field type in the json
370+
return sjson.SetBytes(json, fieldName, []interface{}{fieldType,value})
371+
}
372+
368373
return sjson.SetBytes(json, fieldName, value)
369374
}
370375

abidecoder_test.go

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -469,27 +469,81 @@ func TestABI_decode_StructVariantField(t *testing.T) {
469469
{
470470
Name: "root",
471471
Fields: []FieldDef{
472-
{Name: "name", Type: "variant_"},
472+
{Name: "field", Type: "variant_"},
473473
},
474474
},
475475
},
476476
}
477-
478477
buffer, err := hex.DecodeString("00000050df45e3aec2")
479478
require.NoError(t, err)
479+
json, err := abi.decode(NewDecoder(buffer), "root")
480+
require.NoError(t, err)
481+
assert.JSONEq(t, `{"field": ["name", "serialize"]}`, string(json))
482+
buffer, err = hex.DecodeString("0164000000")
483+
require.NoError(t, err)
484+
json, err = abi.decode(NewDecoder(buffer), "root")
485+
require.NoError(t, err)
486+
assert.JSONEq(t, `{"field":["uint32", 100]}`, string(json))
487+
}
488+
489+
func TestABI_decode_StructArrayOfVariantField_OneOfVariantIsAlias(t *testing.T) {
490+
abi := &ABI{
491+
Types: []ABIType{
492+
ABIType{Type: "name", NewTypeName: "my_name"},
493+
},
494+
Variants: []VariantDef{
495+
{
496+
Name: "variant_",
497+
Types: []string{"my_name", "uint32"},
498+
},
499+
},
500+
Structs: []StructDef{
501+
{
502+
Name: "root",
503+
Fields: []FieldDef{
504+
{Name: "field", Type: "variant_[]"},
505+
},
506+
},
507+
},
508+
}
509+
510+
buffer, err := hex.DecodeString("0200000050df45e3aec20164000000")
511+
require.NoError(t, err)
480512

481513
json, err := abi.decode(NewDecoder(buffer), "root")
482514
require.NoError(t, err)
483515

484-
assert.JSONEq(t, `{"name":"serialize"}`, string(json))
516+
assert.JSONEq(t, `{"field":[["name","serialize"],["uint32",100]]}`, string(json))
517+
}
485518

486-
buffer, err = hex.DecodeString("0164000000")
519+
func TestABI_decode_Struct2DArrayOfVariantField_OneOfVariantIsAlias(t *testing.T) {
520+
abi := &ABI{
521+
Types: []ABIType{
522+
ABIType{Type: "name", NewTypeName: "my_name"},
523+
},
524+
Variants: []VariantDef{
525+
{
526+
Name: "variant_",
527+
Types: []string{"my_name", "uint32"},
528+
},
529+
},
530+
Structs: []StructDef{
531+
{
532+
Name: "root",
533+
Fields: []FieldDef{
534+
{Name: "field", Type: "variant_[][]"},
535+
},
536+
},
537+
},
538+
}
539+
540+
buffer, err := hex.DecodeString("010200000050df45e3aec20164000000")
487541
require.NoError(t, err)
488542

489-
json, err = abi.decode(NewDecoder(buffer), "root")
543+
json, err := abi.decode(NewDecoder(buffer), "root")
490544
require.NoError(t, err)
491545

492-
assert.JSONEq(t, `{"name":100}`, string(json))
546+
assert.JSONEq(t, `{"field":[[["name","serialize"],["uint32",100]]]}`, string(json))
493547
}
494548

495549
func TestABI_decode_StructVariantField_OneOfVariantIsAlias(t *testing.T) {
@@ -507,7 +561,7 @@ func TestABI_decode_StructVariantField_OneOfVariantIsAlias(t *testing.T) {
507561
{
508562
Name: "root",
509563
Fields: []FieldDef{
510-
{Name: "name", Type: "variant_"},
564+
{Name: "field", Type: "variant_"},
511565
},
512566
},
513567
},
@@ -519,15 +573,15 @@ func TestABI_decode_StructVariantField_OneOfVariantIsAlias(t *testing.T) {
519573
json, err := abi.decode(NewDecoder(buffer), "root")
520574
require.NoError(t, err)
521575

522-
assert.JSONEq(t, `{"name":"serialize"}`, string(json))
576+
assert.JSONEq(t, `{"field":["name","serialize"]}`, string(json))
523577

524578
buffer, err = hex.DecodeString("0164000000")
525579
require.NoError(t, err)
526580

527581
json, err = abi.decode(NewDecoder(buffer), "root")
528582
require.NoError(t, err)
529583

530-
assert.JSONEq(t, `{"name":100}`, string(json))
584+
assert.JSONEq(t, `{"field":["uint32",100]}`, string(json))
531585
}
532586

533587
func TestABI_decode_StructAliasToAVariantField(t *testing.T) {
@@ -545,7 +599,7 @@ func TestABI_decode_StructAliasToAVariantField(t *testing.T) {
545599
{
546600
Name: "root",
547601
Fields: []FieldDef{
548-
{Name: "name", Type: "my_variant"},
602+
{Name: "field", Type: "my_variant"},
549603
},
550604
},
551605
},
@@ -557,15 +611,15 @@ func TestABI_decode_StructAliasToAVariantField(t *testing.T) {
557611
json, err := abi.decode(NewDecoder(buffer), "root")
558612
require.NoError(t, err)
559613

560-
assert.JSONEq(t, `{"name":"serialize"}`, string(json))
614+
assert.JSONEq(t, `{"field":["name","serialize"]}`, string(json))
561615

562616
buffer, err = hex.DecodeString("0164000000")
563617
require.NoError(t, err)
564618

565619
json, err = abi.decode(NewDecoder(buffer), "root")
566620
require.NoError(t, err)
567621

568-
assert.JSONEq(t, `{"name":100}`, string(json))
622+
assert.JSONEq(t, `{"field":["uint32",100]}`, string(json))
569623
}
570624

571625
func TestABI_decode_Uint8ArrayVec(t *testing.T) {

0 commit comments

Comments
 (0)