Skip to content

Commit

Permalink
Fix: tezos bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jan 19, 2022
1 parent 571a9a1 commit 9b345a5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tools/tezgen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

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

// UnmarshalJSON -
func (b *Bytes) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
if len(data)%2 == 1 {
return errors.Errorf("invalid bytes value with length %d: %v", data, len(data))
}
if len(data) > 1 {
if data[0] == '"' && data[len(data)-1] == '"' {
data = data[1 : len(data)-1]
}
}
byt := make([]byte, hex.DecodedLen(len(data)))
if _, err := hex.Decode(byt, data); err != nil {
return err
}

*b = make([]byte, 0)
*b = append(*b, byt...)

return nil
}

Expand Down
12 changes: 12 additions & 0 deletions tools/tezgen/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func TestBytes_UnmarshalJSON(t *testing.T) {
name: "test 1",
data: []byte{0x31, 0x32, 0x33, 0x34},
want: []byte{0x12, 0x34},
}, {
name: "test 2",
data: []byte{'"', '"'},
want: []byte{},
}, {
name: "test 3",
data: []byte{0x00},
wantErr: true,
}, {
name: "test 4",
data: []byte{'"', '1', '2', '"'},
want: []byte{0x12},
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 9b345a5

Please sign in to comment.