Created
August 19, 2021 07:57
-
-
Save thomasjo/5db91fe21c68cf30cc3d7e5e8bcdfdb1 to your computer and use it in GitHub Desktop.
Go + YAML "string array" implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://github.com/go-yaml/yaml/issues/100#issuecomment-901604971 | |
type StringArray []string | |
func (a *StringArray) UnmarshalYAML(value *yaml.Node) error { | |
var multi []string | |
err := value.Decode(&multi) | |
if err != nil { | |
var single string | |
err := value.Decode(&single) | |
if err != nil { | |
return err | |
} | |
*a = []string{single} | |
} else { | |
*a = multi | |
} | |
return nil | |
} | |
type Data struct { | |
Field StringArray | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment