bbgo_origin/pkg/bbgo/string.go

58 lines
860 B
Go
Raw Normal View History

2020-10-26 13:45:02 +00:00
package bbgo
2020-10-26 08:15:30 +00:00
import (
"encoding/json"
2020-11-09 08:34:35 +00:00
"fmt"
2020-10-26 08:15:30 +00:00
)
type StringSlice []string
func (s *StringSlice) decode(a interface{}) error {
switch d := a.(type) {
case string:
*s = append(*s, d)
case []string:
*s = append(*s, d...)
case []interface{}:
for _, de := range d {
if err := s.decode(de); err != nil {
return err
}
}
default:
2020-11-09 08:34:35 +00:00
return fmt.Errorf("unexpected type %T for StringSlice: %+v", d, d)
2020-10-26 08:15:30 +00:00
}
return nil
}
func (s *StringSlice) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
var ss []string
err = unmarshal(&ss)
if err == nil {
*s = ss
return
}
var as string
err = unmarshal(&as)
if err == nil {
*s = append(*s, as)
}
return err
}
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)
}