2016-05-08 07:01:45 +00:00
|
|
|
package types
|
2016-04-11 18:15:53 +00:00
|
|
|
|
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 {
|
2016-04-11 18:15:53 +00:00
|
|
|
parts []string
|
|
|
|
}
|
|
|
|
|
2016-05-08 07:01:45 +00:00
|
|
|
// UnmarshalYAML implements custom Yaml unmarshaling.
|
|
|
|
func (s *StringOrSlice) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2016-04-11 18:15:53 +00:00
|
|
|
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 {
|
2016-04-11 18:15:53 +00:00
|
|
|
return s.parts
|
|
|
|
}
|
2016-05-10 05:57:57 +00:00
|
|
|
|
|
|
|
// NewStringOrSlice returns a new StringOrSlice.
|
|
|
|
func NewStringOrSlice(from []string) *StringOrSlice {
|
|
|
|
return &StringOrSlice{from}
|
|
|
|
}
|