Skip to content

Commit 9b4e8bc

Browse files
authored
Merge pull request #117 from eoscanada/feature/support-tx-packed-json-compression-as-int
Added support for int compression type in JSON
2 parents ff94c06 + 7f0e04a commit 9b4e8bc

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
.envrc
22
.idea
3+
4+
# Added temporarly until we actually use go.mod in this lib, so it's possible to easily hack the lib as needed
5+
go.mod

transaction_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package eos
33
import (
44
"encoding/hex"
55
"encoding/json"
6+
"errors"
7+
"fmt"
68
"testing"
79

810
"github.com/stretchr/testify/assert"
@@ -34,3 +36,33 @@ func TestTransactionID(t *testing.T) {
3436
assert.Equal(t, test.expectID, trxID)
3537
}
3638
}
39+
40+
func TestTransaction_UnmarshalPacked_Compression(t *testing.T) {
41+
tests := []struct {
42+
name string
43+
in string
44+
expected CompressionType
45+
expectedErr error
46+
}{
47+
{"string/none", `{"compression": "none"}`, CompressionNone, nil},
48+
{"string/zlib", `{"compression": "zlib"}`, CompressionZlib, nil},
49+
{"string/unknown", `{"compression": "random"}`, 0, errors.New("unknown compression type random")},
50+
51+
{"int/none", `{"compression": 0}`, CompressionNone, nil},
52+
{"int/zlib", `{"compression": 1}`, CompressionZlib, nil},
53+
{"int/unknown", `{"compression": 3}`, 0, errors.New("unknown compression type 3")},
54+
}
55+
56+
for i, test := range tests {
57+
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
58+
var tx *PackedTransaction
59+
err := json.Unmarshal([]byte(test.in), &tx)
60+
if test.expectedErr == nil {
61+
require.NoError(t, err)
62+
assert.Equal(t, test.expected, tx.Compression)
63+
} else {
64+
assert.Equal(t, test.expectedErr, err)
65+
}
66+
})
67+
}
68+
}

types.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,60 @@ func (c CompressionType) MarshalJSON() ([]byte, error) {
8989
}
9090

9191
func (c *CompressionType) UnmarshalJSON(data []byte) error {
92+
tryNext, err := c.tryUnmarshalJSONAsString(data)
93+
if err != nil && !tryNext {
94+
return err
95+
}
96+
97+
if tryNext {
98+
_, err := c.tryUnmarshalJSONAsUint8(data)
99+
if err != nil {
100+
return err
101+
}
102+
}
103+
104+
return nil
105+
}
106+
107+
func (c *CompressionType) tryUnmarshalJSONAsString(data []byte) (tryNext bool, err error) {
92108
var s string
93-
err := json.Unmarshal(data, &s)
109+
err = json.Unmarshal(data, &s)
94110
if err != nil {
95-
return err
111+
_, isTypeError := err.(*json.UnmarshalTypeError)
112+
113+
// Let's continue with next handler is we hit a type error, might be an integer...
114+
return isTypeError, err
96115
}
97116

98117
switch s {
118+
case "none":
119+
*c = CompressionNone
99120
case "zlib":
100121
*c = CompressionZlib
101122
default:
123+
return false, fmt.Errorf("unknown compression type %s", s)
124+
}
125+
126+
return false, nil
127+
}
128+
129+
func (c *CompressionType) tryUnmarshalJSONAsUint8(data []byte) (tryNext bool, err error) {
130+
var s uint8
131+
err = json.Unmarshal(data, &s)
132+
if err != nil {
133+
return false, err
134+
}
135+
136+
switch s {
137+
case 0:
102138
*c = CompressionNone
139+
case 1:
140+
*c = CompressionZlib
141+
default:
142+
return false, fmt.Errorf("unknown compression type %d", s)
103143
}
104-
return nil
144+
145+
return false, nil
105146
}
106147

107148
// CurrencyName

0 commit comments

Comments
 (0)