good morning!!!!

Skip to content
Snippets Groups Projects
Commit c71ca1a0 authored by Taylor Gerring's avatar Taylor Gerring
Browse files

Better nil handling

parent 5d8be9c3
Branches
Tags
No related merge requests found
...@@ -24,10 +24,12 @@ import ( ...@@ -24,10 +24,12 @@ import (
"strings" "strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
) )
type hexdata struct { type hexdata struct {
data []byte data []byte
isNil bool
} }
func (d *hexdata) String() string { func (d *hexdata) String() string {
...@@ -35,6 +37,9 @@ func (d *hexdata) String() string { ...@@ -35,6 +37,9 @@ func (d *hexdata) String() string {
} }
func (d *hexdata) MarshalJSON() ([]byte, error) { func (d *hexdata) MarshalJSON() ([]byte, error) {
if d.isNil {
return json.Marshal(nil)
}
return json.Marshal(d.String()) return json.Marshal(d.String())
} }
...@@ -56,11 +61,19 @@ func newHexData(input interface{}) *hexdata { ...@@ -56,11 +61,19 @@ func newHexData(input interface{}) *hexdata {
case common.Hash: case common.Hash:
d.data = input.Bytes() d.data = input.Bytes()
case *common.Hash: case *common.Hash:
if input == nil {
d.isNil = true
} else {
d.data = input.Bytes() d.data = input.Bytes()
}
case common.Address: case common.Address:
d.data = input.Bytes() d.data = input.Bytes()
// case *common.Address: case *common.Address:
// d.data = input.Bytes() if input == nil {
d.isNil = true
} else {
d.data = input.Bytes()
}
case *big.Int: case *big.Int:
d.data = input.Bytes() d.data = input.Bytes()
case int64: case int64:
...@@ -84,6 +97,7 @@ func newHexData(input interface{}) *hexdata { ...@@ -84,6 +97,7 @@ func newHexData(input interface{}) *hexdata {
type hexnum struct { type hexnum struct {
data []byte data []byte
isNil bool
} }
func (d *hexnum) String() string { func (d *hexnum) String() string {
...@@ -99,6 +113,9 @@ func (d *hexnum) String() string { ...@@ -99,6 +113,9 @@ func (d *hexnum) String() string {
} }
func (d *hexnum) MarshalJSON() ([]byte, error) { func (d *hexnum) MarshalJSON() ([]byte, error) {
if d.isNil {
return json.Marshal(nil)
}
return json.Marshal(d.String()) return json.Marshal(d.String())
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment