2021-12-21 04:02:07 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
2022-10-28 03:16:46 +00:00
|
|
|
"bufio"
|
2022-11-25 01:05:56 +00:00
|
|
|
"bytes"
|
2022-10-28 03:16:46 +00:00
|
|
|
"errors"
|
2021-12-21 04:02:07 +00:00
|
|
|
"io"
|
2022-10-28 03:16:46 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2021-12-21 04:02:07 +00:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type yamlDecoder struct {
|
|
|
|
decoder yaml.Decoder
|
2022-11-25 01:05:56 +00:00
|
|
|
|
|
|
|
prefs YamlPreferences
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
// work around of various parsing issues by yaml.v3 with document headers
|
|
|
|
leadingContent string
|
2022-11-25 01:05:56 +00:00
|
|
|
bufferRead bytes.Buffer
|
|
|
|
|
2023-11-14 02:00:30 +00:00
|
|
|
// anchor map persists over multiple documents for convenience.
|
|
|
|
anchorMap map[string]*CandidateNode
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
readAnything bool
|
|
|
|
firstFile bool
|
|
|
|
documentIndex uint
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func NewYamlDecoder(prefs YamlPreferences) Decoder {
|
|
|
|
return &yamlDecoder{prefs: prefs, firstFile: true}
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func (dec *yamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, string, error) {
|
|
|
|
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
|
2022-11-14 06:22:13 +00:00
|
|
|
var yamlDirectiveLineRegEx = regexp.MustCompile(`^\s*%YA`)
|
2022-10-28 03:16:46 +00:00
|
|
|
var sb strings.Builder
|
|
|
|
for {
|
2023-12-12 00:29:00 +00:00
|
|
|
peekBytes, err := reader.Peek(4)
|
2022-10-28 03:16:46 +00:00
|
|
|
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
|
2022-12-08 02:33:06 +00:00
|
|
|
} else if string(peekBytes[0]) == "\n" {
|
|
|
|
_, err := reader.ReadString('\n')
|
|
|
|
sb.WriteString("\n")
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return reader, sb.String(), nil
|
|
|
|
} else if err != nil {
|
|
|
|
return reader, sb.String(), err
|
|
|
|
}
|
2023-12-12 00:29:00 +00:00
|
|
|
} 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" {
|
2022-10-28 03:16:46 +00:00
|
|
|
_, err := reader.ReadString('\n')
|
2023-09-18 23:52:36 +00:00
|
|
|
sb.WriteString("$yqDocSeparator$\n")
|
2022-10-28 03:16:46 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return reader, sb.String(), nil
|
|
|
|
} else if err != nil {
|
|
|
|
return reader, sb.String(), err
|
|
|
|
}
|
2022-11-14 06:22:13 +00:00
|
|
|
} else if commentLineRegEx.MatchString(string(peekBytes)) || yamlDirectiveLineRegEx.MatchString(string(peekBytes)) {
|
2022-10-28 03:16:46 +00:00
|
|
|
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-28 03:16:46 +00:00
|
|
|
func (dec *yamlDecoder) Init(reader io.Reader) error {
|
|
|
|
readerToUse := reader
|
|
|
|
leadingContent := ""
|
2022-11-25 01:05:56 +00:00
|
|
|
dec.bufferRead = bytes.Buffer{}
|
2022-10-28 03:16:46 +00:00
|
|
|
var err error
|
|
|
|
// 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) {
|
|
|
|
readerToUse, leadingContent, err = dec.processReadStream(bufio.NewReader(reader))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-25 01:05:56 +00:00
|
|
|
} else if !dec.prefs.LeadingContentPreProcessing {
|
|
|
|
// if we're not process the leading content
|
|
|
|
// keep a copy of what we've read. This is incase its a
|
|
|
|
// doc with only comments - the decoder will return nothing
|
|
|
|
// then we can read the comments from bufferRead
|
|
|
|
readerToUse = io.TeeReader(reader, &dec.bufferRead)
|
2022-10-28 03:16:46 +00:00
|
|
|
}
|
|
|
|
dec.leadingContent = leadingContent
|
|
|
|
dec.readAnything = false
|
|
|
|
dec.decoder = *yaml.NewDecoder(readerToUse)
|
|
|
|
dec.firstFile = false
|
2023-10-18 01:11:53 +00:00
|
|
|
dec.documentIndex = 0
|
2023-11-14 02:00:30 +00:00
|
|
|
dec.anchorMap = make(map[string]*CandidateNode)
|
2022-10-28 03:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dec *yamlDecoder) Decode() (*CandidateNode, error) {
|
2023-10-18 01:11:53 +00:00
|
|
|
var yamlNode yaml.Node
|
|
|
|
err := dec.decoder.Decode(&yamlNode)
|
|
|
|
|
2022-10-28 03:16:46 +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
|
2022-11-25 01:05:56 +00:00
|
|
|
} else if errors.Is(err, io.EOF) && !dec.prefs.LeadingContentPreProcessing && !dec.readAnything {
|
|
|
|
// didn't find any yaml,
|
|
|
|
// check the tee buffer, maybe there were comments
|
|
|
|
dec.readAnything = true
|
|
|
|
dec.leadingContent = dec.bufferRead.String()
|
|
|
|
if dec.leadingContent != "" {
|
|
|
|
return dec.blankNodeWithComment(), nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2022-10-28 03:16:46 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
candidateNode := CandidateNode{document: dec.documentIndex}
|
|
|
|
// don't bother with the DocumentNode
|
2023-11-14 02:00:30 +00:00
|
|
|
err = candidateNode.UnmarshalYAML(yamlNode.Content[0], dec.anchorMap)
|
2023-10-18 01:11:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-10-28 03:16:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
candidateNode.HeadComment = yamlNode.HeadComment + candidateNode.HeadComment
|
|
|
|
candidateNode.FootComment = yamlNode.FootComment + candidateNode.FootComment
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
if dec.leadingContent != "" {
|
|
|
|
candidateNode.LeadingContent = dec.leadingContent
|
|
|
|
dec.leadingContent = ""
|
|
|
|
}
|
2022-11-25 01:05:56 +00:00
|
|
|
dec.readAnything = true
|
2023-10-18 01:11:53 +00:00
|
|
|
dec.documentIndex++
|
|
|
|
return &candidateNode, nil
|
2022-10-28 03:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dec *yamlDecoder) blankNodeWithComment() *CandidateNode {
|
2023-10-18 01:11:53 +00:00
|
|
|
node := createScalarNode(nil, "")
|
|
|
|
node.LeadingContent = dec.leadingContent
|
|
|
|
return node
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|