Skip to content

Commit

Permalink
refactor: utils.BigInt should embed a *big.Int after all, not a big.I…
Browse files Browse the repository at this point in the history
…nt. Also, its methods should have pointer receivers, not value receivers. This lets us use big.Int's methods, since they are defined on the pointer

see https://stackoverflow.com/questions/55337900/marshaljson-a-type-with-an-embedded-type-ends-up-as-instead-of-the-value/55338067#55338067
  • Loading branch information
randomshinichi committed Mar 26, 2019
1 parent b9dcdc1 commit 218578a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
18 changes: 9 additions & 9 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,43 +138,43 @@ func AskPassword(question string) (password string, err error) {
// BigInt is composed of a big.Int, but includes a Validate() method for swagger and other convenience functions.
// Once created, it can be used just like a big.Int.
type BigInt struct {
big.Int
*big.Int
}

// Validate ensures that the BigInt's value is >= 0.
// The actual check does not need 'formats' from swagger, which is why Validate() wraps that function.
func (b BigInt) Validate(formats strfmt.Registry) error {
func (b *BigInt) Validate(formats strfmt.Registry) error {
return b.LargerOrEqualToZero()
}

// LargerThanZero checks that the number is >=0
func (b BigInt) LargerThanZero() error {
func (b *BigInt) LargerThanZero() error {
zero := NewBigInt()

if b.Cmp(&zero.Int) != 1 {
if b.Cmp(zero.Int) != 1 {
return fmt.Errorf("%v was not larger than 0", b.Int.String())
}
return nil
}

// LargerOrEqualToZero checks that the number is >=0
func (b BigInt) LargerOrEqualToZero() error {
func (b *BigInt) LargerOrEqualToZero() error {
zero := NewBigInt()

if b.Cmp(&zero.Int) == -1 {
if b.Cmp(zero.Int) == -1 {
return fmt.Errorf("%v was negative", b.Int.String())
}
return nil
}

// NewBigInt returns a new BigInt with its Int struct field initialized
func NewBigInt() (i *BigInt) {
return &BigInt{Int: big.Int{}}
return &BigInt{new(big.Int)}
}

// NewBigIntFromString returns a new BigInt from a string representation
func NewBigIntFromString(number string) (i *BigInt, err error) {
i = &BigInt{Int: big.Int{}}
i = &BigInt{new(big.Int)}
_, success := i.SetString(number, 10)
if success == false {
return nil, errors.New("Could not parse string as a number")
Expand All @@ -193,7 +193,7 @@ func RequireBigIntFromString(number string) *BigInt {

// NewBigIntFromUint64 returns a new BigInt from a uint64 representation
func NewBigIntFromUint64(number uint64) (i *BigInt) {
i = &BigInt{Int: big.Int{}}
i = &BigInt{new(big.Int)}
i.SetUint64(number)
return i
}
14 changes: 7 additions & 7 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

func TestBigInt(t *testing.T) {
var customBig = BigInt{big.Int{}}
var customBig = BigInt{new(big.Int)}
customBig.SetUint64(math.MaxUint64)
fmt.Println(customBig)

var resultBig = BigInt{big.Int{}}
resultBig.Add(&customBig.Int, big.NewInt(1000))
var resultBig = BigInt{new(big.Int)}
resultBig.Add(customBig.Int, big.NewInt(1000))
fmt.Println(resultBig)
}

Expand All @@ -27,11 +27,11 @@ func TestBigIntNewStr(t *testing.T) {
}

// Make a BigInt the old fashioned, raw way.
ex := BigInt{Int: big.Int{}}
ex := BigInt{new(big.Int)}
ex.SetString("20000000000000000000", 10)

// They should equal each other.
if ex.Cmp(&a.Int) != 0 {
if ex.Cmp(a.Int) != 0 {
t.Fatalf("Expected 20000000000000000000 but got %v", a.String())
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func TestBigIntUnmarshalJSON(t *testing.T) {
negBigIntAsString := []byte("-1")
negBigIntAsBigInt := RequireBigIntFromString("-1")

var amount = BigInt{big.Int{}}
var amount = BigInt{new(big.Int)}
var tests = []struct {
input []byte
expected *BigInt
Expand All @@ -104,7 +104,7 @@ func TestBigIntUnmarshalJSON(t *testing.T) {

for _, test := range tests {
amount.UnmarshalJSON(test.input)
if amount.Cmp(&test.expected.Int) != 0 {
if amount.Cmp(test.expected.Int) != 0 {
t.Errorf("Test Failed: %v inputted, %v expected, %#v received", test.input, test.expected, amount)
}
}
Expand Down

0 comments on commit 218578a

Please sign in to comment.