Skip to content

Commit 9b345a5

Browse files
Fix: tezos bytes
1 parent 571a9a1 commit 9b345a5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

tools/tezgen/types.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
jsoniter "github.com/json-iterator/go"
10+
"github.com/pkg/errors"
1011
)
1112

1213
var json = jsoniter.ConfigCompatibleWithStandardLibrary
@@ -19,13 +20,24 @@ type Bytes []byte
1920

2021
// UnmarshalJSON -
2122
func (b *Bytes) UnmarshalJSON(data []byte) error {
23+
if len(data) == 0 {
24+
return nil
25+
}
26+
if len(data)%2 == 1 {
27+
return errors.Errorf("invalid bytes value with length %d: %v", data, len(data))
28+
}
29+
if len(data) > 1 {
30+
if data[0] == '"' && data[len(data)-1] == '"' {
31+
data = data[1 : len(data)-1]
32+
}
33+
}
2234
byt := make([]byte, hex.DecodedLen(len(data)))
2335
if _, err := hex.Decode(byt, data); err != nil {
2436
return err
2537
}
38+
2639
*b = make([]byte, 0)
2740
*b = append(*b, byt...)
28-
2941
return nil
3042
}
3143

tools/tezgen/types_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ func TestBytes_UnmarshalJSON(t *testing.T) {
1717
name: "test 1",
1818
data: []byte{0x31, 0x32, 0x33, 0x34},
1919
want: []byte{0x12, 0x34},
20+
}, {
21+
name: "test 2",
22+
data: []byte{'"', '"'},
23+
want: []byte{},
24+
}, {
25+
name: "test 3",
26+
data: []byte{0x00},
27+
wantErr: true,
28+
}, {
29+
name: "test 4",
30+
data: []byte{'"', '1', '2', '"'},
31+
want: []byte{0x12},
2032
},
2133
}
2234
for _, tt := range tests {

0 commit comments

Comments
 (0)