confgi: fix []interface parsing issue

This commit is contained in:
c9s 2020-10-23 14:01:45 +08:00
parent 9127913370
commit c9f5d51556
3 changed files with 23 additions and 12 deletions

View File

@ -17,8 +17,8 @@ reportPnL:
- "BNBUSDT" - "BNBUSDT"
of: binance of: binance
when: when:
- @daily - "@daily"
- @hourly - "@hourly"
sessions: sessions:
max: max:

View File

@ -18,13 +18,7 @@ type SingleExchangeStrategyConfig struct {
type StringSlice []string type StringSlice []string
func (s *StringSlice) UnmarshalJSON(b []byte) (err error) { func (s *StringSlice) decode(a interface{}) error {
var a interface{}
err = json.Unmarshal(b, &a)
if err != nil {
return err
}
switch d := a.(type) { switch d := a.(type) {
case string: case string:
*s = append(*s, d) *s = append(*s, d)
@ -32,11 +26,28 @@ func (s *StringSlice) UnmarshalJSON(b []byte) (err error) {
case []string: case []string:
*s = append(*s, d...) *s = append(*s, d...)
case []interface{}:
for _, de := range d {
if err := s.decode(de); err != nil {
return err
}
}
default: default:
err = errors.New("unexpected type for StringSlice") return errors.Errorf("unexpected type %T for StringSlice: %+v", d, d)
} }
return err return nil
}
func (s *StringSlice) UnmarshalJSON(b []byte) error {
var a interface{}
var err = json.Unmarshal(b, &a)
if err != nil {
return err
}
return s.decode(a)
} }
type PnLReporter struct { type PnLReporter struct {

View File

@ -9,7 +9,7 @@ import (
_ "github.com/c9s/bbgo/pkg/strategy/buyandhold" _ "github.com/c9s/bbgo/pkg/strategy/buyandhold"
) )
func TestLoadStrategies(t *testing.T) { func TestLoadConfig(t *testing.T) {
type args struct { type args struct {
configFile string configFile string
} }