From 218578a809e7e1dcb57373e1e05401150a8fd2ce Mon Sep 17 00:00:00 2001 From: Andrew Chiw Date: Tue, 26 Mar 2019 01:21:00 +0100 Subject: [PATCH] refactor: utils.BigInt should embed a *big.Int after all, not a big.Int. 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 --- utils/utils.go | 18 +++++++++--------- utils/utils_test.go | 14 +++++++------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index ab4ed266..843c47b2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -138,30 +138,30 @@ 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 @@ -169,12 +169,12 @@ func (b BigInt) LargerOrEqualToZero() error { // 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") @@ -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 } diff --git a/utils/utils_test.go b/utils/utils_test.go index 120fa12e..63170a08 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -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) } @@ -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()) } @@ -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 @@ -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) } }