diff options
author | Felix Lange <fjl@twurst.com> | 2017-02-23 00:59:59 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2017-03-02 21:05:46 +0800 |
commit | d304da380382245190f33878363696cd151201a9 (patch) | |
tree | ae1791944e79a9ea741fb447a6346309e4b016c7 /common/types.go | |
parent | 357d00cdb1625e4a715e0b3928ef7bd3656a8d95 (diff) | |
download | dexon-d304da380382245190f33878363696cd151201a9.tar dexon-d304da380382245190f33878363696cd151201a9.tar.gz dexon-d304da380382245190f33878363696cd151201a9.tar.bz2 dexon-d304da380382245190f33878363696cd151201a9.tar.lz dexon-d304da380382245190f33878363696cd151201a9.tar.xz dexon-d304da380382245190f33878363696cd151201a9.tar.zst dexon-d304da380382245190f33878363696cd151201a9.zip |
common/hexutil: implement TextMarshaler, TextUnmarshaler
This commit makes the wrapper types more generally applicable.
encoding.TextMarshaler is supported by most codec implementations (e.g.
for yaml).
The tests now ensure that package json actually recognizes the custom
marshaler implementation irrespective of how it is implemented.
The Uint type has new tests, too. These are tricky because uint size
depends on the CPU word size. Turns out that there was one incorrect
case where decoding returned ErrUint64Range instead of ErrUintRange.
Diffstat (limited to 'common/types.go')
-rw-r--r-- | common/types.go | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/common/types.go b/common/types.go index 0c9e68903..9c50beb13 100644 --- a/common/types.go +++ b/common/types.go @@ -71,14 +71,14 @@ func (h Hash) Format(s fmt.State, c rune) { fmt.Fprintf(s, "%"+string(c), h[:]) } -// UnmarshalJSON parses a hash in its hex from to a hash. -func (h *Hash) UnmarshalJSON(input []byte) error { - return hexutil.UnmarshalJSON("Hash", input, h[:]) +// UnmarshalText parses a hash in hex syntax. +func (h *Hash) UnmarshalText(input []byte) error { + return hexutil.UnmarshalFixedText("Hash", input, h[:]) } -// Serialize given hash to JSON -func (h Hash) MarshalJSON() ([]byte, error) { - return hexutil.Bytes(h[:]).MarshalJSON() +// MarshalText returns the hex representation of h. +func (h Hash) MarshalText() ([]byte, error) { + return hexutil.Bytes(h[:]).MarshalText() } // Sets the hash to the value of b. If b is larger than len(h) it will panic @@ -171,14 +171,14 @@ func (a *Address) Set(other Address) { } } -// Serialize given address to JSON -func (a Address) MarshalJSON() ([]byte, error) { - return hexutil.Bytes(a[:]).MarshalJSON() +// MarshalText returns the hex representation of a. +func (a Address) MarshalText() ([]byte, error) { + return hexutil.Bytes(a[:]).MarshalText() } -// Parse address from raw json data -func (a *Address) UnmarshalJSON(input []byte) error { - return hexutil.UnmarshalJSON("Address", input, a[:]) +// UnmarshalText parses a hash in hex syntax. +func (a *Address) UnmarshalText(input []byte) error { + return hexutil.UnmarshalFixedText("Address", input, a[:]) } // PP Pretty Prints a byte slice in the following format: |