Skip to content

Commit 1846d02

Browse files
Fix: tezos bytes
1 parent 9b345a5 commit 1846d02

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

tools/tezgen/types.go

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

99
jsoniter "github.com/json-iterator/go"
10-
"github.com/pkg/errors"
1110
)
1211

1312
var json = jsoniter.ConfigCompatibleWithStandardLibrary
@@ -20,24 +19,16 @@ type Bytes []byte
2019

2120
// UnmarshalJSON -
2221
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-
}
34-
byt := make([]byte, hex.DecodedLen(len(data)))
35-
if _, err := hex.Decode(byt, data); err != nil {
22+
var x string
23+
if err := json.Unmarshal(data, &x); err != nil {
3624
return err
3725
}
3826

39-
*b = make([]byte, 0)
40-
*b = append(*b, byt...)
27+
str, err := hex.DecodeString(x)
28+
if err != nil {
29+
return err
30+
}
31+
*b = Bytes(str)
4132
return nil
4233
}
4334

tools/tezgen/types_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ func TestBytes_UnmarshalJSON(t *testing.T) {
1515
}{
1616
{
1717
name: "test 1",
18-
data: []byte{0x31, 0x32, 0x33, 0x34},
19-
want: []byte{0x12, 0x34},
20-
}, {
21-
name: "test 2",
2218
data: []byte{'"', '"'},
2319
want: []byte{},
2420
}, {
25-
name: "test 3",
21+
name: "test 2",
2622
data: []byte{0x00},
2723
wantErr: true,
2824
}, {
29-
name: "test 4",
25+
name: "test 3",
3026
data: []byte{'"', '1', '2', '"'},
3127
want: []byte{0x12},
3228
},

0 commit comments

Comments
 (0)