From 3a948b2dbaf22409507a2d3244032751b76432bb Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 31 Mar 2015 17:39:58 +0200 Subject: Add hexdata and hexnum types --- rpc/messages.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'rpc/messages.go') diff --git a/rpc/messages.go b/rpc/messages.go index 5c498234f..108a07ed8 100644 --- a/rpc/messages.go +++ b/rpc/messages.go @@ -19,8 +19,84 @@ package rpc import ( "encoding/json" "fmt" + "math/big" + "strings" + + "github.com/ethereum/go-ethereum/common" ) +type hexdata struct { + data []byte +} + +func (d *hexdata) MarshalJSON() ([]byte, error) { + v := common.Bytes2Hex(d.data) + return json.Marshal("0x" + v) +} + +func (d *hexdata) UnmarshalJSON(b []byte) (err error) { + d.data = common.FromHex(string(b)) + return nil +} + +func newHexData(input interface{}) *hexdata { + d := new(hexdata) + + switch input.(type) { + case []byte: + d.data = input.([]byte) + case common.Hash: + d.data = input.(common.Hash).Bytes() + case common.Address: + d.data = input.(common.Address).Bytes() + case *big.Int: + d.data = input.(*big.Int).Bytes() + case int64: + d.data = big.NewInt(input.(int64)).Bytes() + case uint64: + d.data = big.NewInt(int64(input.(uint64))).Bytes() + case int: + d.data = big.NewInt(int64(input.(int))).Bytes() + case uint: + d.data = big.NewInt(int64(input.(uint))).Bytes() + case string: + d.data = common.Big(input.(string)).Bytes() + default: + d.data = nil + } + + return d +} + +type hexnum struct { + data []byte +} + +func (d *hexnum) MarshalJSON() ([]byte, error) { + // Get hex string from bytes + out := common.Bytes2Hex(d.data) + // Trim leading 0s + out = strings.Trim(out, "0") + // Output "0x0" when value is 0 + if len(out) == 0 { + out = "0" + } + return json.Marshal("0x" + out) +} + +func (d *hexnum) UnmarshalJSON(b []byte) (err error) { + d.data = common.FromHex(string(b)) + return nil +} + +func newHexNum(input interface{}) *hexnum { + d := new(hexnum) + + d.data = newHexData(input).data + + return d +} + type InvalidTypeError struct { method string msg string -- cgit v1.2.3 From a2501ecfcd0709db8bd43ecdc4077d072230fb28 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 31 Mar 2015 19:02:46 +0200 Subject: Make new types Stringers --- rpc/messages.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'rpc/messages.go') diff --git a/rpc/messages.go b/rpc/messages.go index 108a07ed8..1ad41654b 100644 --- a/rpc/messages.go +++ b/rpc/messages.go @@ -29,9 +29,12 @@ type hexdata struct { data []byte } +func (d *hexdata) String() string { + return "0x" + common.Bytes2Hex(d.data) +} + func (d *hexdata) MarshalJSON() ([]byte, error) { - v := common.Bytes2Hex(d.data) - return json.Marshal("0x" + v) + return json.Marshal(d.String()) } func (d *hexdata) UnmarshalJSON(b []byte) (err error) { @@ -72,7 +75,7 @@ type hexnum struct { data []byte } -func (d *hexnum) MarshalJSON() ([]byte, error) { +func (d *hexnum) String() string { // Get hex string from bytes out := common.Bytes2Hex(d.data) // Trim leading 0s @@ -81,7 +84,11 @@ func (d *hexnum) MarshalJSON() ([]byte, error) { if len(out) == 0 { out = "0" } - return json.Marshal("0x" + out) + return "0x" + out +} + +func (d *hexnum) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) } func (d *hexnum) UnmarshalJSON(b []byte) (err error) { -- cgit v1.2.3 From 7b7392826d7b76fd4a0bd57baa7ca1224a19a3f6 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 11:38:06 +0200 Subject: Improved response tests Actually verifies output as by regex --- rpc/messages.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'rpc/messages.go') diff --git a/rpc/messages.go b/rpc/messages.go index 1ad41654b..42af53c58 100644 --- a/rpc/messages.go +++ b/rpc/messages.go @@ -50,8 +50,12 @@ func newHexData(input interface{}) *hexdata { d.data = input.([]byte) case common.Hash: d.data = input.(common.Hash).Bytes() + case *common.Hash: + d.data = input.(*common.Hash).Bytes() case common.Address: d.data = input.(common.Address).Bytes() + case *common.Address: + d.data = input.(*common.Address).Bytes() case *big.Int: d.data = input.(*big.Int).Bytes() case int64: @@ -62,7 +66,7 @@ func newHexData(input interface{}) *hexdata { d.data = big.NewInt(int64(input.(int))).Bytes() case uint: d.data = big.NewInt(int64(input.(uint))).Bytes() - case string: + case string: // hexstring d.data = common.Big(input.(string)).Bytes() default: d.data = nil -- cgit v1.2.3 From b860b6769329137c24024c2330529d7b2078d89d Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 11:45:29 +0200 Subject: Remove extra type assetion --- rpc/messages.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'rpc/messages.go') diff --git a/rpc/messages.go b/rpc/messages.go index 42af53c58..f868c5f9b 100644 --- a/rpc/messages.go +++ b/rpc/messages.go @@ -45,29 +45,29 @@ func (d *hexdata) UnmarshalJSON(b []byte) (err error) { func newHexData(input interface{}) *hexdata { d := new(hexdata) - switch input.(type) { + switch input := input.(type) { case []byte: - d.data = input.([]byte) + d.data = input case common.Hash: - d.data = input.(common.Hash).Bytes() + d.data = input.Bytes() case *common.Hash: - d.data = input.(*common.Hash).Bytes() + d.data = input.Bytes() case common.Address: - d.data = input.(common.Address).Bytes() + d.data = input.Bytes() case *common.Address: - d.data = input.(*common.Address).Bytes() + d.data = input.Bytes() case *big.Int: - d.data = input.(*big.Int).Bytes() + d.data = input.Bytes() case int64: - d.data = big.NewInt(input.(int64)).Bytes() + d.data = big.NewInt(input).Bytes() case uint64: - d.data = big.NewInt(int64(input.(uint64))).Bytes() + d.data = big.NewInt(int64(input)).Bytes() case int: - d.data = big.NewInt(int64(input.(int))).Bytes() + d.data = big.NewInt(int64(input)).Bytes() case uint: - d.data = big.NewInt(int64(input.(uint))).Bytes() + d.data = big.NewInt(int64(input)).Bytes() case string: // hexstring - d.data = common.Big(input.(string)).Bytes() + d.data = common.Big(input).Bytes() default: d.data = nil } -- cgit v1.2.3