harness-drone/yaml/types/slice.go

36 lines
810 B
Go
Raw Normal View History

2016-05-08 07:01:45 +00:00
package types
2016-05-08 07:01:45 +00:00
// StringOrSlice is a custom Yaml type that can hold a string or slice of strings.
type StringOrSlice struct {
parts []string
}
2016-05-08 07:01:45 +00:00
// UnmarshalYAML implements custom Yaml unmarshaling.
func (s *StringOrSlice) UnmarshalYAML(unmarshal func(interface{}) error) error {
var sliceType []string
err := unmarshal(&sliceType)
if err == nil {
s.parts = sliceType
return nil
}
var stringType string
err = unmarshal(&stringType)
if err == nil {
sliceType = make([]string, 0, 1)
s.parts = append(sliceType, string(stringType))
return nil
}
return err
}
2016-05-08 07:01:45 +00:00
// Slice returns the slice of strings.
func (s StringOrSlice) Slice() []string {
return s.parts
}
2016-05-10 05:57:57 +00:00
// NewStringOrSlice returns a new StringOrSlice.
func NewStringOrSlice(from []string) *StringOrSlice {
return &StringOrSlice{from}
}