include yaml directives #1424

This commit is contained in:
Mike Farah 2022-11-14 17:22:13 +11:00
parent 63db5de4e2
commit 762f46ed54
2 changed files with 20 additions and 1 deletions

View File

@ -34,6 +34,24 @@ EOM
assertEquals "$expected" "$X" 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() { testLeadingSeperatorPipeIntoEvalSeq() {
X=$(./yq e - < test.yml) X=$(./yq e - < test.yml)
expected=$(cat test.yml) expected=$(cat test.yml)

View File

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