Files
gocryptotrader/exchanges/margin/margin.go
Scott 6ee26a7da1 exchanges/margin: Fix marshalling issue (#1812)
* Fixes issue with marshalling margin types

* Update exchanges/order/order_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/margin/margin.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/order/order_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update test name

---------

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
2025-03-06 15:17:05 +11:00

81 lines
1.9 KiB
Go

package margin
import (
"fmt"
"strings"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
// Valid returns whether the margin type is valid
func (t Type) Valid() bool {
return t != Unset && supported&t == t
}
// UnmarshalJSON converts json into margin type
func (t *Type) UnmarshalJSON(d []byte) error {
var marginType string
err := json.Unmarshal(d, &marginType)
if err != nil {
return err
}
*t, err = StringToMarginType(marginType)
return err
}
// MarshalJSON conforms type to the json.Marshaler interface
func (t Type) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
// String returns the string representation of the margin type in lowercase
// the absence of a lower func should hopefully highlight that String is lower
func (t Type) String() string {
switch t {
case Unset:
return unsetStr
case Isolated:
return isolatedStr
case Multi:
return multiStr
case SpotIsolated:
return spotIsolatedStr
case NoMargin:
return cashStr
default:
return unknownStr
}
}
// Upper returns the upper case string representation of the margin type
func (t Type) Upper() string {
return strings.ToUpper(t.String())
}
// IsValidString checks to see if the supplied string is a valid margin type
func IsValidString(m string) bool {
switch strings.ToLower(m) {
case isolatedStr, multiStr, unsetStr, crossedStr, crossStr, spotIsolatedStr, cashStr:
return true
}
return false
}
// StringToMarginType converts a string to a margin type
// doesn't error, just returns unknown if the string is not recognised
func StringToMarginType(m string) (Type, error) {
switch strings.ToLower(m) {
case isolatedStr:
return Isolated, nil
case multiStr, crossedStr, crossStr:
return Multi, nil
case spotIsolatedStr:
return SpotIsolated, nil
case cashStr:
return NoMargin, nil
case "":
return Unset, nil
}
return Unknown, fmt.Errorf("%w %v", ErrInvalidMarginType, m)
}