Skip to content

Commit

Permalink
include yaml directives #1424
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Nov 14, 2022
1 parent 63db5de commit 762f46e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 18 additions & 0 deletions acceptance_tests/leading-seperator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ EOM
assertEquals "$expected" "$X"
}

testLeadingSeperatorWithDirective() {
cat >test.yml <<EOL
%YAML 1.1
---
this: should really work
EOL

read -r -d '' expected << EOM
%YAML 1.1
---
this: should really work
EOM

X=$(./yq < test.yml)
assertEquals "$expected" "$X"
}


testLeadingSeperatorPipeIntoEvalSeq() {
X=$(./yq e - < test.yml)
expected=$(cat test.yml)
Expand Down
3 changes: 2 additions & 1 deletion pkg/yqlib/decoder_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewYamlDecoder(prefs YamlPreferences) Decoder {

func (dec *yamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, string, error) {
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
var yamlDirectiveLineRegEx = regexp.MustCompile(`^\s*%YA`)
var sb strings.Builder
for {
peekBytes, err := reader.Peek(3)
Expand All @@ -41,7 +42,7 @@ func (dec *yamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, stri
} else if err != nil {
return reader, sb.String(), err
}
} else if commentLineRegEx.MatchString(string(peekBytes)) {
} else if commentLineRegEx.MatchString(string(peekBytes)) || yamlDirectiveLineRegEx.MatchString(string(peekBytes)) {
line, err := reader.ReadString('\n')
sb.WriteString(line)
if errors.Is(err, io.EOF) {
Expand Down

0 comments on commit 762f46e

Please sign in to comment.