Skip to content

Commit

Permalink
Merge pull request #117 from eoscanada/feature/support-tx-packed-json…
Browse files Browse the repository at this point in the history
…-compression-as-int

Added support for int compression type in JSON
  • Loading branch information
maoueh authored Mar 8, 2019
2 parents ff94c06 + 7f0e04a commit 9b4e8bc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.envrc
.idea

# Added temporarly until we actually use go.mod in this lib, so it's possible to easily hack the lib as needed
go.mod
32 changes: 32 additions & 0 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package eos
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -34,3 +36,33 @@ func TestTransactionID(t *testing.T) {
assert.Equal(t, test.expectID, trxID)
}
}

func TestTransaction_UnmarshalPacked_Compression(t *testing.T) {
tests := []struct {
name string
in string
expected CompressionType
expectedErr error
}{
{"string/none", `{"compression": "none"}`, CompressionNone, nil},
{"string/zlib", `{"compression": "zlib"}`, CompressionZlib, nil},
{"string/unknown", `{"compression": "random"}`, 0, errors.New("unknown compression type random")},

{"int/none", `{"compression": 0}`, CompressionNone, nil},
{"int/zlib", `{"compression": 1}`, CompressionZlib, nil},
{"int/unknown", `{"compression": 3}`, 0, errors.New("unknown compression type 3")},
}

for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
var tx *PackedTransaction
err := json.Unmarshal([]byte(test.in), &tx)
if test.expectedErr == nil {
require.NoError(t, err)
assert.Equal(t, test.expected, tx.Compression)
} else {
assert.Equal(t, test.expectedErr, err)
}
})
}
}
47 changes: 44 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,60 @@ func (c CompressionType) MarshalJSON() ([]byte, error) {
}

func (c *CompressionType) UnmarshalJSON(data []byte) error {
tryNext, err := c.tryUnmarshalJSONAsString(data)
if err != nil && !tryNext {
return err
}

if tryNext {
_, err := c.tryUnmarshalJSONAsUint8(data)
if err != nil {
return err
}
}

return nil
}

func (c *CompressionType) tryUnmarshalJSONAsString(data []byte) (tryNext bool, err error) {
var s string
err := json.Unmarshal(data, &s)
err = json.Unmarshal(data, &s)
if err != nil {
return err
_, isTypeError := err.(*json.UnmarshalTypeError)

// Let's continue with next handler is we hit a type error, might be an integer...
return isTypeError, err
}

switch s {
case "none":
*c = CompressionNone
case "zlib":
*c = CompressionZlib
default:
return false, fmt.Errorf("unknown compression type %s", s)
}

return false, nil
}

func (c *CompressionType) tryUnmarshalJSONAsUint8(data []byte) (tryNext bool, err error) {
var s uint8
err = json.Unmarshal(data, &s)
if err != nil {
return false, err
}

switch s {
case 0:
*c = CompressionNone
case 1:
*c = CompressionZlib
default:
return false, fmt.Errorf("unknown compression type %d", s)
}
return nil

return false, nil
}

// CurrencyName
Expand Down

0 comments on commit 9b4e8bc

Please sign in to comment.