2021-12-21 04:02:07 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
2022-10-25 03:22:16 +00:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
2021-12-21 04:02:07 +00:00
|
|
|
"io"
|
2022-10-25 03:22:16 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2021-12-21 04:02:07 +00:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type yamlDecoder struct {
|
|
|
|
decoder yaml.Decoder
|
2022-10-25 03:22:16 +00:00
|
|
|
// work around of various parsing issues by yaml.v3 with document headers
|
2022-10-25 04:35:49 +00:00
|
|
|
prefs YamlPreferences
|
2022-10-25 03:22:16 +00:00
|
|
|
leadingContent string
|
2022-10-27 05:16:00 +00:00
|
|
|
readAnything bool
|
2022-10-28 02:50:22 +00:00
|
|
|
firstFile bool
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 04:35:49 +00:00
|
|
|
func NewYamlDecoder(prefs YamlPreferences) Decoder {
|
2022-10-28 02:50:22 +00:00
|
|
|
return &yamlDecoder{prefs: prefs, firstFile: true}
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 03:22:16 +00:00
|
|
|
func (dec *yamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, string, error) {
|
|
|
|
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
|
|
|
|
var sb strings.Builder
|
|
|
|
for {
|
|
|
|
peekBytes, err := reader.Peek(3)
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
// EOF are handled else where..
|
|
|
|
return reader, sb.String(), nil
|
|
|
|
} else if err != nil {
|
|
|
|
return reader, sb.String(), err
|
|
|
|
} else if string(peekBytes) == "---" {
|
|
|
|
_, err := reader.ReadString('\n')
|
|
|
|
sb.WriteString("$yqDocSeperator$\n")
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return reader, sb.String(), nil
|
|
|
|
} else if err != nil {
|
|
|
|
return reader, sb.String(), err
|
|
|
|
}
|
|
|
|
} else if commentLineRegEx.MatchString(string(peekBytes)) {
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
sb.WriteString(line)
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return reader, sb.String(), nil
|
|
|
|
} else if err != nil {
|
|
|
|
return reader, sb.String(), err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return reader, sb.String(), nil
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 03:22:16 +00:00
|
|
|
func (dec *yamlDecoder) Init(reader io.Reader) error {
|
|
|
|
readerToUse := reader
|
|
|
|
leadingContent := ""
|
|
|
|
var err error
|
2022-10-28 02:50:22 +00:00
|
|
|
// if we 'evaluating together' - we only process the leading content
|
|
|
|
// of the first file - this ensures comments from subsequent files are
|
|
|
|
// merged together correctly.
|
|
|
|
if dec.prefs.LeadingContentPreProcessing && (!dec.prefs.EvaluateTogether || dec.firstFile) {
|
2022-10-25 03:22:16 +00:00
|
|
|
readerToUse, leadingContent, err = dec.processReadStream(bufio.NewReader(reader))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dec.leadingContent = leadingContent
|
2022-10-27 05:16:00 +00:00
|
|
|
dec.readAnything = false
|
2022-10-25 03:22:16 +00:00
|
|
|
dec.decoder = *yaml.NewDecoder(readerToUse)
|
2022-10-28 02:50:22 +00:00
|
|
|
dec.firstFile = false
|
2022-10-25 03:22:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dec *yamlDecoder) Decode() (*CandidateNode, error) {
|
|
|
|
var dataBucket yaml.Node
|
|
|
|
|
|
|
|
err := dec.decoder.Decode(&dataBucket)
|
2022-10-27 05:16:00 +00:00
|
|
|
if errors.Is(err, io.EOF) && dec.leadingContent != "" && !dec.readAnything {
|
|
|
|
// force returning an empty node with a comment.
|
|
|
|
dec.readAnything = true
|
|
|
|
return dec.blankNodeWithComment(), nil
|
|
|
|
|
|
|
|
} else if err != nil {
|
2022-10-25 03:22:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
candidateNode := &CandidateNode{
|
|
|
|
Node: &dataBucket,
|
|
|
|
}
|
|
|
|
|
|
|
|
if dec.leadingContent != "" {
|
|
|
|
candidateNode.LeadingContent = dec.leadingContent
|
|
|
|
dec.leadingContent = ""
|
|
|
|
}
|
|
|
|
// move document comments into candidate node
|
|
|
|
// otherwise unwrap drops them.
|
|
|
|
candidateNode.TrailingContent = dataBucket.FootComment
|
|
|
|
dataBucket.FootComment = ""
|
|
|
|
return candidateNode, nil
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|
2022-10-27 05:16:00 +00:00
|
|
|
|
|
|
|
func (dec *yamlDecoder) blankNodeWithComment() *CandidateNode {
|
|
|
|
return &CandidateNode{
|
|
|
|
Document: 0,
|
|
|
|
Filename: "",
|
|
|
|
Node: &yaml.Node{Kind: yaml.DocumentNode, Content: []*yaml.Node{{Tag: "!!null", Kind: yaml.ScalarNode}}},
|
|
|
|
FileIndex: 0,
|
|
|
|
LeadingContent: dec.leadingContent,
|
|
|
|
}
|
|
|
|
}
|