Skip to content

Commit

Permalink
using default value method
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed May 2, 2024
1 parent b3e6bfd commit 47d91f5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions genesis/customnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ func (i *HexOrDecimal256) UnmarshalJSON(input []byte) error {
}

// MarshalJSON implements the json.Marshaler interface.
func (i *HexOrDecimal256) MarshalJSON() ([]byte, error) {
text, err := (*math.HexOrDecimal256)(i).MarshalText()
func (i HexOrDecimal256) MarshalJSON() ([]byte, error) {
decimal256 := math.HexOrDecimal256(i)
text, err := decimal256.MarshalText()
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion genesis/customnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ func TestHexOrDecimal256MarshalUnmarshal(t *testing.T) {
assert.Equal(t, originalHex, string(directMarshallJson))

// using json overloading ( satisfies the json.Unmarshal interface )
// using value
marshalVal, err := json.Marshal(unmarshaledValue)
assert.NoError(t, err, "Marshaling should not produce an error")
assert.NotEqual(t, originalHex, string(marshalVal))
assert.Equal(t, originalHex, string(marshalVal))

// using json overloading ( satisfies the json.Unmarshal interface )
// using pointer
marshalPtr, err := json.Marshal(&unmarshaledValue)
assert.NoError(t, err, "Marshaling should not produce an error")
assert.Equal(t, originalHex, string(marshalPtr))
Expand Down
4 changes: 2 additions & 2 deletions thor/bytes32.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (b Bytes32) IsZero() bool {
}

// MarshalJSON implements json.Marshaler.
func (b *Bytes32) MarshalJSON() ([]byte, error) {
if b == nil {
func (b Bytes32) MarshalJSON() ([]byte, error) {
if b == [32]byte{} {
return json.Marshal(nil)
}

Expand Down
5 changes: 3 additions & 2 deletions thor/bytes32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ func TestMarshalUnmarshall(t *testing.T) {
assert.Equal(t, originalHex, string(directMarshallJson))

// using json overloading ( satisfies the json.Unmarshal interface )
// direct value does not marshal correctly
// using value
marshalVal, err := json.Marshal(unmarshaledValue)
assert.NoError(t, err)
assert.NotEqual(t, originalHex, string(marshalVal))
assert.Equal(t, originalHex, string(marshalVal))

// using json overloading ( satisfies the json.Unmarshal interface )
// using pointer
marshalPtr, err := json.Marshal(&unmarshaledValue)
assert.NoError(t, err, "Marshaling should not produce an error")
assert.Equal(t, originalHex, string(marshalPtr))
Expand Down

0 comments on commit 47d91f5

Please sign in to comment.