Skip to content

Commit

Permalink
added hash and address MarshallText() (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgensheff authored Apr 22, 2024
1 parent d646d5f commit 301648e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
15 changes: 15 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str)
return h
}

// MarshalText implements TextMarshaler interface for Address
func (a Address) MarshalText() (text []byte, err error) {
return []byte(a.String()), nil
}

// UnmarshalText decodes the form generated by MarshalText
func (a *Address) UnmarshalText(text []byte) error {
if len(text) == 0 {
*a = Address{}
return nil
}
*a = BytesToAddress(FromHex(string(text)))
return nil
}
15 changes: 15 additions & 0 deletions types/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ func (h *Hash) SetBytes(b []byte) {
// BigToHash sets byte representation of b to hash.
// If b is larger than len(h), b will be cropped from the left.
func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }

// MarshalText implements TextMarshaler interface for Hash
func (h Hash) MarshalText() (text []byte, err error) {
return []byte(h.String()), nil
}

// UnmarshalText decodes the form generated by MarshalText
func (h *Hash) UnmarshalText(text []byte) error {
if len(text) == 0 {
*h = Hash{}
return nil
}
*h = BytesToHash(FromHex(string(text)))
return nil
}
75 changes: 75 additions & 0 deletions types/types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/json"
"math/big"
"testing"
)
Expand Down Expand Up @@ -29,3 +30,77 @@ func TestHash_Compare(t *testing.T) {
t.Fatal("incorrect uint64 conversion")
}
}

func TestHash_MarshalText(t *testing.T) {
m := map[Hash]Hash{
{1}: {2},
}

b, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}

if string(b) != "{\"0x0100000000000000000000000000000000000000000000000000000000000000\":\"0x0200000000000000000000000000000000000000000000000000000000000000\"}" {
t.Fatal("incorrect marshalling")
}
}

func TestHash_UnmarshalText(t *testing.T) {
b := []byte("{\"0x0100000000000000000000000000000000000000000000000000000000000000\":\"0x0200000000000000000000000000000000000000000000000000000000000000\"}")

exp := map[Hash]Hash{
{1}: {2},
}

m := make(map[Hash]Hash)

err := json.Unmarshal(b, &m)
if err != nil {
t.Fatal(err)
}

for k, v := range m {
if exp[k] != v {
t.Fatal("incorrect marshalling")
}
}
}

func TestAddress_MarshalText(t *testing.T) {
addr := HexToAddress("0x9c1a711a5e31a9461f6d1f662068e0a2f9edf552")
m := map[Address]Address{
addr: addr,
}

b, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}

if string(b) != "{\"0x9c1a711a5e31a9461f6d1f662068e0a2f9edf552\":\"0x9c1a711a5e31a9461f6d1f662068e0a2f9edf552\"}" {
t.Fatal("incorrect marshalling")
}
}

func TestAddress_UnmarshalText(t *testing.T) {
b := []byte("{\"0x9c1a711a5e31a9461f6d1f662068e0a2f9edf552\":\"0x9c1a711a5e31a9461f6d1f662068e0a2f9edf552\"}")

addr := HexToAddress("0x9c1a711a5e31a9461f6d1f662068e0a2f9edf552")
exp := map[Address]Address{
addr: addr,
}

m := make(map[Address]Address)

err := json.Unmarshal(b, &m)
if err != nil {
t.Fatal(err)
}

for k, v := range m {
if exp[k] != v {
t.Fatal("incorrect marshalling")
}
}
}

0 comments on commit 301648e

Please sign in to comment.