Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added hash and address MarshallText() #72

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -66,3 +66,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")
}
}
}
Loading