Skip to content

Commit

Permalink
Fix Variant decoding, added a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jubeless committed Apr 29, 2020
1 parent ed8e922 commit c0dd979
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
### Added
- Ability to decode nested `arrays`

### Changed
* 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]}`
* 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"`.
5 changes: 5 additions & 0 deletions abidecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ func (a *ABI) read(binaryDecoder *Decoder, fieldName string, fieldType string, j
abiDecoderLog.Debug("set field value", zap.String("name", fieldName), zap.Reflect("value", value))
}

if variant != nil {
// As a variant we need to include the field type in the json
return sjson.SetBytes(json, fieldName, []interface{}{fieldType,value})
}

return sjson.SetBytes(json, fieldName, value)
}

Expand Down
78 changes: 66 additions & 12 deletions abidecoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,27 +469,81 @@ func TestABI_decode_StructVariantField(t *testing.T) {
{
Name: "root",
Fields: []FieldDef{
{Name: "name", Type: "variant_"},
{Name: "field", Type: "variant_"},
},
},
},
}

buffer, err := hex.DecodeString("00000050df45e3aec2")
require.NoError(t, err)
json, err := abi.decode(NewDecoder(buffer), "root")
require.NoError(t, err)
assert.JSONEq(t, `{"field": ["name", "serialize"]}`, string(json))
buffer, err = hex.DecodeString("0164000000")
require.NoError(t, err)
json, err = abi.decode(NewDecoder(buffer), "root")
require.NoError(t, err)
assert.JSONEq(t, `{"field":["uint32", 100]}`, string(json))
}

func TestABI_decode_StructArrayOfVariantField_OneOfVariantIsAlias(t *testing.T) {
abi := &ABI{
Types: []ABIType{
ABIType{Type: "name", NewTypeName: "my_name"},
},
Variants: []VariantDef{
{
Name: "variant_",
Types: []string{"my_name", "uint32"},
},
},
Structs: []StructDef{
{
Name: "root",
Fields: []FieldDef{
{Name: "field", Type: "variant_[]"},
},
},
},
}

buffer, err := hex.DecodeString("0200000050df45e3aec20164000000")
require.NoError(t, err)

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

assert.JSONEq(t, `{"name":"serialize"}`, string(json))
assert.JSONEq(t, `{"field":[["name","serialize"],["uint32",100]]}`, string(json))
}

buffer, err = hex.DecodeString("0164000000")
func TestABI_decode_Struct2DArrayOfVariantField_OneOfVariantIsAlias(t *testing.T) {
abi := &ABI{
Types: []ABIType{
ABIType{Type: "name", NewTypeName: "my_name"},
},
Variants: []VariantDef{
{
Name: "variant_",
Types: []string{"my_name", "uint32"},
},
},
Structs: []StructDef{
{
Name: "root",
Fields: []FieldDef{
{Name: "field", Type: "variant_[][]"},
},
},
},
}

buffer, err := hex.DecodeString("010200000050df45e3aec20164000000")
require.NoError(t, err)

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

assert.JSONEq(t, `{"name":100}`, string(json))
assert.JSONEq(t, `{"field":[[["name","serialize"],["uint32",100]]]}`, string(json))
}

func TestABI_decode_StructVariantField_OneOfVariantIsAlias(t *testing.T) {
Expand All @@ -507,7 +561,7 @@ func TestABI_decode_StructVariantField_OneOfVariantIsAlias(t *testing.T) {
{
Name: "root",
Fields: []FieldDef{
{Name: "name", Type: "variant_"},
{Name: "field", Type: "variant_"},
},
},
},
Expand All @@ -519,15 +573,15 @@ func TestABI_decode_StructVariantField_OneOfVariantIsAlias(t *testing.T) {
json, err := abi.decode(NewDecoder(buffer), "root")
require.NoError(t, err)

assert.JSONEq(t, `{"name":"serialize"}`, string(json))
assert.JSONEq(t, `{"field":["name","serialize"]}`, string(json))

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

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

assert.JSONEq(t, `{"name":100}`, string(json))
assert.JSONEq(t, `{"field":["uint32",100]}`, string(json))
}

func TestABI_decode_StructAliasToAVariantField(t *testing.T) {
Expand All @@ -545,7 +599,7 @@ func TestABI_decode_StructAliasToAVariantField(t *testing.T) {
{
Name: "root",
Fields: []FieldDef{
{Name: "name", Type: "my_variant"},
{Name: "field", Type: "my_variant"},
},
},
},
Expand All @@ -557,15 +611,15 @@ func TestABI_decode_StructAliasToAVariantField(t *testing.T) {
json, err := abi.decode(NewDecoder(buffer), "root")
require.NoError(t, err)

assert.JSONEq(t, `{"name":"serialize"}`, string(json))
assert.JSONEq(t, `{"field":["name","serialize"]}`, string(json))

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

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

assert.JSONEq(t, `{"name":100}`, string(json))
assert.JSONEq(t, `{"field":["uint32",100]}`, string(json))
}

func TestABI_decode_Uint8ArrayVec(t *testing.T) {
Expand Down

0 comments on commit c0dd979

Please sign in to comment.