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"
of: binance
when:
- @daily
- @hourly
- "@daily"
- "@hourly"
sessions:
max:

View File

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

View File

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