mirror of https://github.com/k3s-io/k3s
Merge pull request #34297 from AdoHe/fix_yaml_decoder
Automatic merge from submit-queue fix yaml decode issue fix #33588 @janetkuo @smarterclayton ptal.pull/6/head
commit
538915c037
|
@ -137,7 +137,7 @@ func (d *YAMLDecoder) Close() error {
|
|||
}
|
||||
|
||||
const yamlSeparator = "\n---"
|
||||
const separator = "---\n"
|
||||
const separator = "---"
|
||||
|
||||
// splitYAMLDocument is a bufio.SplitFunc for splitting YAML streams into individual documents.
|
||||
func splitYAMLDocument(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
|
@ -246,16 +246,28 @@ func (r *YAMLReader) Read() ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if string(line) == separator || err == io.EOF {
|
||||
sep := len([]byte(separator))
|
||||
if i := bytes.Index(line, []byte(separator)); i == 0 {
|
||||
// We have a potential document terminator
|
||||
i += sep
|
||||
after := line[i:]
|
||||
if len(strings.TrimRightFunc(string(after), unicode.IsSpace)) == 0 {
|
||||
if buffer.Len() != 0 {
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
if err == io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err == io.EOF {
|
||||
if buffer.Len() != 0 {
|
||||
// If we're at EOF, we have a final, non-terminated line. Return it.
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
if err == io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
buffer.Write(line)
|
||||
return nil, err
|
||||
}
|
||||
buffer.Write(line)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -106,7 +106,7 @@ func TestDecodeYAML(t *testing.T) {
|
|||
s := NewYAMLToJSONDecoder(bytes.NewReader([]byte(`---
|
||||
stuff: 1
|
||||
|
||||
---
|
||||
---
|
||||
`)))
|
||||
obj := generic{}
|
||||
if err := s.Decode(&obj); err != nil {
|
||||
|
@ -138,11 +138,11 @@ stuff: 1
|
|||
obj := generic{}
|
||||
err := s.Decode(&obj)
|
||||
if err == nil {
|
||||
t.Fatal("expected error with yaml: prefix, got no error")
|
||||
t.Fatal("expected error with yaml: violate, got no error")
|
||||
}
|
||||
fmt.Printf("err: %s\n", err.Error())
|
||||
if !strings.HasPrefix(err.Error(), "yaml: line 1:") {
|
||||
t.Fatalf("expected %q to have 'yaml: line 1:' prefix", err.Error())
|
||||
if !strings.HasPrefix(err.Error(), "yaml: line 2:") {
|
||||
t.Fatalf("expected %q to have 'yaml: line 2:' found a tab character", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue