Files
gocryptotrader/exchanges/margin/margin.go
Ryan O'Hara-Reid e99adca86f encoding/json: Add custom JSON package with build tag support for Sonic (#1623)
* tag optional sonic and allow full library conversion

* Add workflow and disallow arm and darwin usage

* Add basic hotswap benchmark

* linter: fix

* use bash

* linter: fix?

* Fix whoopsie, add to make file, also add mention in features list.

* test enforcement

* actually read documentation see if this works

* linter: fix

* linter: fix

* sonic: bump tagged version

* encoding/json: drop build tag arch and os filters

* encoding/json: consolidate tests

* encoding/json: log build tag usage

* rm superfluous builds

* glorious/nits: add template change and regen docs

* glorious/nits: update commentary on nolint directive

* glorious/nits: rm init func and log results in main.go

* Test to actually pull flag in

* linter: fix

* thrasher: nits

* gk: nits 4 goflags goooooooooo!

* gk: nits rn

* make sonic default json implementation

* screen 386

* linter: fix

* Add commentary

* glorious: nits Makefile not working

* gk: nits

* gk: nits whoops

* whoopsirino

* mention 32bit systems won't be sonic

* gk: super-duper nit of extremes

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
2025-02-20 16:05:55 +11:00

77 lines
1.8 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
}
// 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
case Unknown:
return unknownStr
}
return ""
}
// 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)
}