Skip to content

Commit

Permalink
fixup! add balance type
Browse files Browse the repository at this point in the history
  • Loading branch information
narumiruna committed Mar 4, 2024
1 parent 75d856b commit 4c7471c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
24 changes: 10 additions & 14 deletions pkg/types/balance_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,29 @@ const (

var ErrInvalidBalanceType = errors.New("invalid balance type")

func (b BalanceType) Validate() error {
func ParseBalanceType(s string) (b BalanceType, err error) {
b = BalanceType(strings.ToUpper(s))
switch b {
case BalanceTypeAvailable:
case BalanceTypeLocked:
case BalanceTypeBorrowed:
case BalanceTypeInterest:
case BalanceTypeNet:
case BalanceTypeTotal:
case BalanceTypeDebt:
default:
return ErrInvalidBalanceType
case BalanceTypeAvailable, BalanceTypeLocked, BalanceTypeBorrowed, BalanceTypeInterest, BalanceTypeNet, BalanceTypeTotal, BalanceTypeDebt:
return b, nil
}
return nil
return b, ErrInvalidBalanceType
}

func (p *BalanceType) UnmarshalJSON(data []byte) error {
func (b *BalanceType) UnmarshalJSON(data []byte) error {
var s string

if err := json.Unmarshal(data, &s); err != nil {
return err
}

*p = BalanceType(strings.ToUpper(s))
if err := p.Validate(); err != nil {
t, err := ParseBalanceType(s)
if err != nil {
return err
}

*b = t

return nil
}

Expand Down
21 changes: 9 additions & 12 deletions pkg/types/price_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ const (

var ErrInvalidPriceType = errors.New("invalid price type")

func (p PriceType) Validate() error {
func ParsePriceType(s string) (p PriceType, err error) {
p = PriceType(strings.ToUpper(s))
switch p {
case PriceTypeLast:
case PriceTypeBuy:
case PriceTypeSell:
case PriceTypeMid:
case PriceTypeMaker:
case PriceTypeTaker:
default:
return ErrInvalidPriceType
case PriceTypeLast, PriceTypeBuy, PriceTypeSell, PriceTypeMid, PriceTypeMaker, PriceTypeTaker:
return p, err
}
return nil
return p, ErrInvalidPriceType
}

func (p *PriceType) UnmarshalJSON(data []byte) error {
Expand All @@ -42,11 +37,13 @@ func (p *PriceType) UnmarshalJSON(data []byte) error {
return err
}

*p = PriceType(strings.ToUpper(s))
if err := p.Validate(); err != nil {
t, err := ParsePriceType(s)
if err != nil {
return err
}

*p = t

return nil
}

Expand Down

0 comments on commit 4c7471c

Please sign in to comment.