mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
58 lines
860 B
Go
58 lines
860 B
Go
package bbgo
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
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:
|
|
return fmt.Errorf("unexpected type %T for StringSlice: %+v", d, d)
|
|
}
|
|
|
|
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)
|
|
}
|