Skip to content

Commit

Permalink
Fixed handling of --- #1890, #1896
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Dec 12, 2023
1 parent fa322ae commit 7f26d91
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/yqlib/decoder_yaml.go
Expand Up @@ -37,7 +37,7 @@ func (dec *yamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, stri
var yamlDirectiveLineRegEx = regexp.MustCompile(`^\s*%YA`)
var sb strings.Builder
for {
peekBytes, err := reader.Peek(3)
peekBytes, err := reader.Peek(4)
if errors.Is(err, io.EOF) {
// EOF are handled else where..
return reader, sb.String(), nil
Expand All @@ -51,7 +51,15 @@ func (dec *yamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, stri
} else if err != nil {
return reader, sb.String(), err
}
} else if string(peekBytes) == "---" {
} else if string(peekBytes) == "--- " {
_, err := reader.ReadString(' ')
sb.WriteString("$yqDocSeparator$\n")
if errors.Is(err, io.EOF) {
return reader, sb.String(), nil
} else if err != nil {
return reader, sb.String(), err
}
} else if string(peekBytes) == "---\n" {
_, err := reader.ReadString('\n')
sb.WriteString("$yqDocSeparator$\n")
if errors.Is(err, io.EOF) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/yqlib/yaml_test.go
Expand Up @@ -7,6 +7,18 @@ import (
)

var yamlFormatScenarios = []formatScenario{
{
description: "scalar with doc separator",
skipDoc: true,
input: "--- cat",
expected: "---\ncat\n",
},
{
description: "scalar with doc separator",
skipDoc: true,
input: "---cat",
expected: "---cat\n",
},
{
description: "basic - null",
skipDoc: true,
Expand Down

0 comments on commit 7f26d91

Please sign in to comment.