bbgo: use enumer to generate enumer parser

This commit is contained in:
c9s 2022-09-01 13:28:00 +08:00
parent 3d32faff46
commit 8cd646668a
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 141 additions and 5 deletions

5
go.mod
View File

@ -71,6 +71,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denisenkom/go-mssqldb v0.12.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dmarkham/enumer v1.5.6 // indirect
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
@ -101,6 +102,7 @@ require (
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pascaldekloe/name v1.0.0 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
@ -123,11 +125,12 @@ require (
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.9 // indirect
golang.org/x/tools v0.1.11 // indirect
google.golang.org/genproto v0.0.0-20220405205423-9d709892a2bf // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

8
go.sum
View File

@ -130,6 +130,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dmarkham/enumer v1.5.6 h1:afhpzVOu8PoBL/+4J07PxVBf9cNnSawS/jAZK1snyLw=
github.com/dmarkham/enumer v1.5.6/go.mod h1:eAawajOQnFBxf0NndBKgbqJImkHytg3eFEngUovqgo8=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@ -495,6 +497,8 @@ github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/name v1.0.0 h1:n7LKFgHixETzxpRv2R77YgPUFo85QHGZKrdaYm7eY5U=
github.com/pascaldekloe/name v1.0.0/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
@ -728,6 +732,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@ -922,6 +928,8 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -0,0 +1,117 @@
// Code generated by "enumer -type=BackTestFeeMode -transform=snake -trimprefix BackTestFeeMode -yaml -json"; DO NOT EDIT.
package bbgo
import (
"encoding/json"
"fmt"
"strings"
)
const _BackTestFeeModeName = "quotenativetoken"
var _BackTestFeeModeIndex = [...]uint8{0, 5, 11, 16}
const _BackTestFeeModeLowerName = "quotenativetoken"
func (i BackTestFeeMode) String() string {
if i < 0 || i >= BackTestFeeMode(len(_BackTestFeeModeIndex)-1) {
return fmt.Sprintf("BackTestFeeMode(%d)", i)
}
return _BackTestFeeModeName[_BackTestFeeModeIndex[i]:_BackTestFeeModeIndex[i+1]]
}
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
func _BackTestFeeModeNoOp() {
var x [1]struct{}
_ = x[BackTestFeeModeQuote-(0)]
_ = x[BackTestFeeModeNative-(1)]
_ = x[BackTestFeeModeToken-(2)]
}
var _BackTestFeeModeValues = []BackTestFeeMode{BackTestFeeModeQuote, BackTestFeeModeNative, BackTestFeeModeToken}
var _BackTestFeeModeNameToValueMap = map[string]BackTestFeeMode{
_BackTestFeeModeName[0:5]: BackTestFeeModeQuote,
_BackTestFeeModeLowerName[0:5]: BackTestFeeModeQuote,
_BackTestFeeModeName[5:11]: BackTestFeeModeNative,
_BackTestFeeModeLowerName[5:11]: BackTestFeeModeNative,
_BackTestFeeModeName[11:16]: BackTestFeeModeToken,
_BackTestFeeModeLowerName[11:16]: BackTestFeeModeToken,
}
var _BackTestFeeModeNames = []string{
_BackTestFeeModeName[0:5],
_BackTestFeeModeName[5:11],
_BackTestFeeModeName[11:16],
}
// BackTestFeeModeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func BackTestFeeModeString(s string) (BackTestFeeMode, error) {
if val, ok := _BackTestFeeModeNameToValueMap[s]; ok {
return val, nil
}
if val, ok := _BackTestFeeModeNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to BackTestFeeMode values", s)
}
// BackTestFeeModeValues returns all values of the enum
func BackTestFeeModeValues() []BackTestFeeMode {
return _BackTestFeeModeValues
}
// BackTestFeeModeStrings returns a slice of all String values of the enum
func BackTestFeeModeStrings() []string {
strs := make([]string, len(_BackTestFeeModeNames))
copy(strs, _BackTestFeeModeNames)
return strs
}
// IsABackTestFeeMode returns "true" if the value is listed in the enum definition. "false" otherwise
func (i BackTestFeeMode) IsABackTestFeeMode() bool {
for _, v := range _BackTestFeeModeValues {
if i == v {
return true
}
}
return false
}
// MarshalJSON implements the json.Marshaler interface for BackTestFeeMode
func (i BackTestFeeMode) MarshalJSON() ([]byte, error) {
return json.Marshal(i.String())
}
// UnmarshalJSON implements the json.Unmarshaler interface for BackTestFeeMode
func (i *BackTestFeeMode) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("BackTestFeeMode should be a string, got %s", data)
}
var err error
*i, err = BackTestFeeModeString(s)
return err
}
// MarshalYAML implements a YAML Marshaler for BackTestFeeMode
func (i BackTestFeeMode) MarshalYAML() (interface{}, error) {
return i.String(), nil
}
// UnmarshalYAML implements a YAML Unmarshaler for BackTestFeeMode
func (i *BackTestFeeMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
var err error
*i, err = BackTestFeeModeString(s)
return err
}

View File

@ -101,22 +101,23 @@ type Session struct {
IsolatedMarginSymbol string `json:"isolatedMarginSymbol,omitempty" yaml:"isolatedMarginSymbol,omitempty"`
}
type BackTestFeeMode string
//go:generate go run github.com/dmarkham/enumer -type=BackTestFeeMode -transform=snake -trimprefix BackTestFeeMode -yaml -json
type BackTestFeeMode int
const (
// BackTestFeeModeQuoteFee is designed for clean position but which also counts the fee in the quote balance.
// buy order = quote currency fee
// sell order = quote currency fee
BackTestFeeModeQuoteFee BackTestFeeMode = "quote_fee"
BackTestFeeModeQuote BackTestFeeMode = iota // quote
// BackTestFeeModeNativeFee is the default crypto exchange fee mode.
// buy order = base currency fee
// sell order = quote currency fee
BackTestFeeModeNativeFee BackTestFeeMode = "native"
BackTestFeeModeNative // BackTestFeeMode = "native"
// BackTestFeeModeFeeToken is the mode which calculates fee from the outside of the balances.
// the fee will not be included in the balances nor the profit.
BackTestFeeModeFeeToken BackTestFeeMode = "fee_token"
BackTestFeeModeToken // BackTestFeeMode = "token"
)
type BackTest struct {

View File

@ -254,6 +254,13 @@ func TestSyncSymbol(t *testing.T) {
})
}
func TestBackTestFeeMode(t *testing.T) {
var mode BackTestFeeMode
var err = yaml.Unmarshal([]byte(`quote`), &mode)
assert.NoError(t, err)
assert.Equal(t, BackTestFeeModeQuote, mode)
}
func Test_categorizeSyncSymbol(t *testing.T) {
var ss []SyncSymbol
var err = yaml.Unmarshal([]byte(`