Files
gocryptotrader/exchanges/collateral/collateral.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

75 lines
1.9 KiB
Go

package collateral
import (
"fmt"
"strings"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
// Valid returns whether the collateral mode is valid
func (t Mode) Valid() bool {
return t != UnsetMode && supportedCollateralModes&t == t
}
// UnmarshalJSON converts json into collateral mode
func (t *Mode) UnmarshalJSON(d []byte) error {
var mode string
err := json.Unmarshal(d, &mode)
if err != nil {
return err
}
*t, err = StringToMode(mode)
return err
}
// String returns the string representation of the collateral mode in lowercase
// the absence of a lower func should hopefully highlight that String is lower
func (t Mode) String() string {
switch t {
case UnsetMode:
return unsetCollateralStr
case SingleMode:
return singleCollateralStr
case MultiMode:
return multiCollateralStr
case PortfolioMode:
return portfolioCollateralStr
case SpotFuturesMode:
return spotFuturesCollateralStr
case UnknownMode:
return unknownCollateralStr
}
return ""
}
// Upper returns the upper case string representation of the collateral mode
func (t Mode) Upper() string {
return strings.ToUpper(t.String())
}
// IsValidCollateralModeString checks to see if the supplied string is a valid collateral mode
func IsValidCollateralModeString(m string) bool {
switch strings.ToLower(m) {
case singleCollateralStr, multiCollateralStr, portfolioCollateralStr, unsetCollateralStr:
return true
}
return false
}
// StringToMode converts a string to a collateral mode
// doesn't error, just returns unknown if the string is not recognised
func StringToMode(m string) (Mode, error) {
switch strings.ToLower(m) {
case singleCollateralStr:
return SingleMode, nil
case multiCollateralStr:
return MultiMode, nil
case portfolioCollateralStr:
return PortfolioMode, nil
case "":
return UnsetMode, nil
}
return UnknownMode, fmt.Errorf("%w %v", ErrInvalidCollateralMode, m)
}