Skip to content

Commit 571a9a1

Browse files
Fix: tezgen types.Bytes
1 parent d998cfb commit 571a9a1

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

tools/tezgen/types.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@ type Bytes []byte
1919

2020
// UnmarshalJSON -
2121
func (b *Bytes) UnmarshalJSON(data []byte) error {
22-
var str string
23-
if err := json.Unmarshal(data, &str); err != nil {
24-
return err
25-
}
26-
byt, err := hex.DecodeString(str)
27-
if err != nil {
22+
byt := make([]byte, hex.DecodedLen(len(data)))
23+
if _, err := hex.Decode(byt, data); err != nil {
2824
return err
2925
}
3026
*b = make([]byte, 0)

tools/tezgen/types_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tezgen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestBytes_UnmarshalJSON(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
data []byte
13+
want []byte
14+
wantErr bool
15+
}{
16+
{
17+
name: "test 1",
18+
data: []byte{0x31, 0x32, 0x33, 0x34},
19+
want: []byte{0x12, 0x34},
20+
},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
var b Bytes
25+
if err := json.Unmarshal(tt.data, &b); (err != nil) != tt.wantErr {
26+
t.Errorf("Bytes.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
27+
return
28+
}
29+
assert.Equal(t, tt.want, []byte(b))
30+
})
31+
}
32+
}

0 commit comments

Comments
 (0)