How to decode yaml with ---
fanux opened this issue · 2 comments
fanux commented
kind: Namespace
metadata:
name: test
---
name: aaa
type Namespace struct {
Kind string `json:"kind"`
Metadata struct {
Name string `json:"name"`
} `json:"metadata"`
}
type Name struct {
Name string `json:"name"`
}
Can't decode the name struct, How?
sitnikovv commented
maybe crutch...
re := regexp.MustCompile("(?m)^\\s*---\\s*$")
text = re.ReplaceAllString(text, "")
but, it is damages multiline text if it contains --- only on one row
fanux commented
I see kubernetes done it like this:
//YamlHandler is
func YamlHandler(rawBytes []byte, fn YamlCallback) (err error) {
reader := bytes.NewReader(rawBytes)
ext := runtime.RawExtension{}
d := yaml.NewYAMLOrJSONDecoder(reader, 4096)
for {
if err = d.Decode(&ext); err != nil {
if err == io.EOF {
return nil
}
return fmt.Errorf("decode yaml json failed: %v", err)
}
//Raw is already to json
if err = fn(ext.Raw); err != nil {
return fmt.Errorf("handler yaml callback fn failed: %v", err)
}
}
}
NewYAMLOrJSONDecoder split the yaml file