pkg/types: refactor exchange name

This commit is contained in:
Edwin 2023-11-17 16:01:15 +08:00
parent 4f224c1c2a
commit f46ca57bb2

View File

@ -15,30 +15,6 @@ const DateFormat = "2006-01-02"
type ExchangeName string
func (n *ExchangeName) Value() (driver.Value, error) {
return n.String(), nil
}
func (n *ExchangeName) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case "binance", "bitget", "bybit", "max", "okex", "kucoin":
*n = ExchangeName(s)
return nil
}
return fmt.Errorf("unknown or unsupported exchange name: %s, valid names are: binance, bitget, bybit, max, okex, kucoin", s)
}
func (n ExchangeName) String() string {
return string(n)
}
const (
ExchangeMax ExchangeName = "max"
ExchangeBinance ExchangeName = "binance"
@ -59,15 +35,43 @@ var SupportedExchanges = []ExchangeName{
// note: we are not using "backtest"
}
func ValidExchangeName(a string) (ExchangeName, error) {
aa := strings.ToLower(a)
for _, n := range SupportedExchanges {
if string(n) == aa {
return n, nil
}
func (n *ExchangeName) Value() (driver.Value, error) {
return n.String(), nil
}
func (n *ExchangeName) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
return "", fmt.Errorf("invalid exchange name: %s", a)
*n = ExchangeName(s)
if !n.IsValid() {
return fmt.Errorf("%s is an invalid exchange name", s)
}
return nil
}
func (n ExchangeName) IsValid() bool {
switch n {
case ExchangeBinance, ExchangeBitget, ExchangeBybit, ExchangeMax, ExchangeOKEx, ExchangeKucoin:
return true
}
return false
}
func (n ExchangeName) String() string {
return string(n)
}
func ValidExchangeName(a string) (ExchangeName, error) {
exName := ExchangeName(strings.ToLower(a))
if !exName.IsValid() {
return "", fmt.Errorf("invalid exchange name: %s", a)
}
return exName, nil
}
type ExchangeMinimal interface {