Skip to content

Commit

Permalink
refactor: BigInt LargerThanZero functions return a bool, not an error
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Jun 4, 2019
1 parent 1079858 commit 454d0a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
20 changes: 13 additions & 7 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,34 @@ type BigInt struct {
// 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 {
return b.LargerOrEqualToZero()
v := b.LargerOrEqualToZero()
if !v {
return fmt.Errorf("%v was not >=0", b.Int.String())
}
return nil
}

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

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

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

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

// UnmarshalJSON ensures that BigInt.Int is always initialized, even if the JSON value is nil.
func (b *BigInt) UnmarshalJSON(text []byte) error {
if b.Int == nil {
b.Int = &big.Int{}
Expand Down
25 changes: 12 additions & 13 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"errors"
"fmt"
"math"
"math/big"
Expand Down Expand Up @@ -41,13 +40,13 @@ func TestBigIntLargerOrEqualToZero(t *testing.T) {
var amount = NewBigInt()
var tests = []struct {
input int64
expected error
expected bool
}{
{math.MinInt64, errors.New("-9223372036854775808 was negative")},
{-1, errors.New("-1 was negative")},
{0, nil},
{1, nil},
{math.MaxInt64, nil},
{math.MinInt64, false},
{-1, false},
{0, true},
{1, true},
{math.MaxInt64, true},
}

for _, test := range tests {
Expand All @@ -63,13 +62,13 @@ func TestBigIntLargerThanZero(t *testing.T) {
var amount = NewBigInt()
var tests = []struct {
input int64
expected error
expected bool
}{
{math.MinInt64, errors.New("-9223372036854775808 was negative")},
{-1, errors.New("-1 was negative")},
{0, errors.New("0 was not larger than 0")},
{1, nil},
{math.MaxInt64, nil},
{math.MinInt64, false},
{-1, false},
{0, false},
{1, true},
{math.MaxInt64, true},
}

for _, test := range tests {
Expand Down

0 comments on commit 454d0a7

Please sign in to comment.