Better way of processing leading content

This commit is contained in:
Mike Farah 2021-07-20 10:19:55 +10:00
parent bbebebe30c
commit 4e628327c4
9 changed files with 92 additions and 62 deletions

View File

@ -248,8 +248,7 @@ a: test
b: sane b: sane
EOM EOM
X=$(./yq e '... comments=""' test.yml)
X=$(./yq e --header-preprocess=false '... comments=""' test.yml)
assertEquals "$expected" "$X" assertEquals "$expected" "$X"
} }
@ -265,9 +264,7 @@ a: test
b: sane b: sane
EOL EOL
# it will be hard to remove that top level separator
read -r -d '' expected << EOM read -r -d '' expected << EOM
---
# hi peeps # hi peeps
# cool # cool
a: test a: test

View File

@ -55,7 +55,7 @@ See https://mikefarah.gitbook.io/yq/ for detailed documentation and examples.`,
rootCmd.PersistentFlags().BoolVarP(&forceColor, "colors", "C", false, "force print with colors") rootCmd.PersistentFlags().BoolVarP(&forceColor, "colors", "C", false, "force print with colors")
rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no colors") rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no colors")
rootCmd.PersistentFlags().StringVarP(&frontMatter, "front-matter", "f", "", "(extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact") rootCmd.PersistentFlags().StringVarP(&frontMatter, "front-matter", "f", "", "(extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact")
rootCmd.PersistentFlags().BoolVarP(&leadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and seperators before processing expression. This is a workaround for go-yaml to persist header content. You will want this off if you want to remove leading comments.") rootCmd.PersistentFlags().BoolVarP(&leadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and seperators before processing expression. This is a workaround for go-yaml to persist header content. This flag will be removed once this feature has been out in the wild for a while.")
rootCmd.AddCommand( rootCmd.AddCommand(
createEvaluateSequenceCommand(), createEvaluateSequenceCommand(),
createEvaluateAllCommand(), createEvaluateAllCommand(),

View File

@ -2,7 +2,3 @@
# hi peeps # hi peeps
# cool # cool
a: test a: test
---
# this is another doc
# great
b: sane

View File

@ -73,15 +73,17 @@ func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string
candidateNode := &CandidateNode{ candidateNode := &CandidateNode{
Document: 0, Document: 0,
Filename: "", Filename: "",
Node: &yaml.Node{Tag: "!!null", Kind: yaml.ScalarNode}, Node: &yaml.Node{Kind: yaml.DocumentNode, HeadComment: firstFileLeadingContent, Content: []*yaml.Node{{Tag: "!!null", Kind: yaml.ScalarNode}}},
FileIndex: 0, FileIndex: 0,
} }
allDocuments.PushBack(candidateNode) allDocuments.PushBack(candidateNode)
} else {
allDocuments.Front().Value.(*CandidateNode).Node.HeadComment = firstFileLeadingContent
} }
matches, err := e.EvaluateCandidateNodes(expression, allDocuments) matches, err := e.EvaluateCandidateNodes(expression, allDocuments)
if err != nil { if err != nil {
return err return err
} }
return printer.PrintResults(matches, firstFileLeadingContent) return printer.PrintResults(matches)
} }

View File

@ -256,7 +256,7 @@ func documentOutput(t *testing.T, w *bufio.Writer, s expressionScenario, formatt
t.Error(err, s.expression) t.Error(err, s.expression)
} }
err = printer.PrintResults(context.MatchingNodes, "") err = printer.PrintResults(context.MatchingNodes)
if err != nil { if err != nil {
t.Error(err, s.expression) t.Error(err, s.expression)
} }

View File

@ -10,7 +10,7 @@ import (
) )
type Printer interface { type Printer interface {
PrintResults(matchingNodes *list.List, leadingContent string) error PrintResults(matchingNodes *list.List) error
PrintedAnything() bool PrintedAnything() bool
//e.g. when given a front-matter doc, like jekyll //e.g. when given a front-matter doc, like jekyll
SetAppendix(reader io.Reader) SetAppendix(reader io.Reader)
@ -85,7 +85,7 @@ func (p *resultsPrinter) safelyFlush(writer *bufio.Writer) {
} }
} }
func (p *resultsPrinter) PrintResults(matchingNodes *list.List, leadingContent string) error { func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
log.Debug("PrintResults for %v matches", matchingNodes.Len()) log.Debug("PrintResults for %v matches", matchingNodes.Len())
if p.outputToJSON { if p.outputToJSON {
explodeOp := Operation{OperationType: explodeOpType} explodeOp := Operation{OperationType: explodeOpType}
@ -101,9 +101,6 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List, leadingContent s
defer p.safelyFlush(bufferedWriter) defer p.safelyFlush(bufferedWriter)
if matchingNodes.Len() == 0 { if matchingNodes.Len() == 0 {
if err := p.writeString(bufferedWriter, leadingContent); err != nil {
return err
}
log.Debug("no matching results, nothing to print") log.Debug("no matching results, nothing to print")
return nil return nil
} }
@ -114,32 +111,52 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List, leadingContent s
p.firstTimePrinting = false p.firstTimePrinting = false
} }
printedLead := false
for el := matchingNodes.Front(); el != nil; el = el.Next() { for el := matchingNodes.Front(); el != nil; el = el.Next() {
mappedDoc := el.Value.(*CandidateNode) mappedDoc := el.Value.(*CandidateNode)
log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators) log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators)
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators &&
(printedLead || !strings.HasPrefix(leadingContent, "---")) { commentStartsWithSeparator := strings.Contains(mappedDoc.Node.HeadComment, "$yqLeadingContent$\n$yqDocSeperator$")
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators && !commentStartsWithSeparator {
log.Debug("-- writing doc sep") log.Debug("-- writing doc sep")
if err := p.writeString(bufferedWriter, "---\n"); err != nil { if err := p.writeString(bufferedWriter, "---\n"); err != nil {
return err return err
} }
} }
if !printedLead { if strings.Contains(mappedDoc.Node.HeadComment, "$yqLeadingContent$") {
// dont print leading comments and seperator if: log.Debug("headcommentwas %v", mappedDoc.Node.HeadComment)
// - we are print json; or log.Debug("finished headcomment")
// - we are printing an unwrapped scalar node reader := bufio.NewReader(strings.NewReader(mappedDoc.Node.HeadComment))
if !p.outputToJSON && (mappedDoc.Node.Kind != yaml.ScalarNode || !p.unwrapScalar) { mappedDoc.Node.HeadComment = ""
// we want to print this after the seperator logic
if err := p.writeString(bufferedWriter, leadingContent); err != nil { for {
return err
readline, errReading := reader.ReadString('\n')
if errReading != nil && errReading != io.EOF {
return errReading
}
if strings.Contains(readline, "$yqLeadingContent$") {
// skip this
} else if strings.Contains(readline, "$yqDocSeperator$") {
if p.printDocSeparators {
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
return err
}
}
} else if !p.outputToJSON {
if err := p.writeString(bufferedWriter, readline); err != nil {
return err
}
}
if errReading == io.EOF {
break
} }
} }
printedLead = true
}
}
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil { if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {
return err return err
} }

View File

@ -52,17 +52,17 @@ func TestPrinterMultipleDocsInSequence(t *testing.T) {
el = el.Next() el = el.Next()
sample3 := nodeToList(el.Value.(*CandidateNode)) sample3 := nodeToList(el.Value.(*CandidateNode))
err = printer.PrintResults(sample1, "") err = printer.PrintResults(sample1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample2, "") err = printer.PrintResults(sample2)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample3, "") err = printer.PrintResults(sample3)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -82,25 +82,28 @@ func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) {
} }
el := inputs.Front() el := inputs.Front()
el.Value.(*CandidateNode).Node.HeadComment = "$yqLeadingContent$\n# go cats\n$yqDocSeperator$\n"
sample1 := nodeToList(el.Value.(*CandidateNode)) sample1 := nodeToList(el.Value.(*CandidateNode))
el = el.Next() el = el.Next()
el.Value.(*CandidateNode).Node.HeadComment = "$yqLeadingContent$\n$yqDocSeperator$\n"
sample2 := nodeToList(el.Value.(*CandidateNode)) sample2 := nodeToList(el.Value.(*CandidateNode))
el = el.Next() el = el.Next()
el.Value.(*CandidateNode).Node.HeadComment = "$yqLeadingContent$\n$yqDocSeperator$\n# cool\n"
sample3 := nodeToList(el.Value.(*CandidateNode)) sample3 := nodeToList(el.Value.(*CandidateNode))
err = printer.PrintResults(sample1, "# go cats\n---\n") err = printer.PrintResults(sample1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample2, "---\n") err = printer.PrintResults(sample2)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample3, "---\n# cool\n") err = printer.PrintResults(sample3)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -138,17 +141,17 @@ func TestPrinterMultipleFilesInSequence(t *testing.T) {
elNode.FileIndex = 2 elNode.FileIndex = 2
sample3 := nodeToList(elNode) sample3 := nodeToList(elNode)
err = printer.PrintResults(sample1, "") err = printer.PrintResults(sample1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample2, "") err = printer.PrintResults(sample2)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample3, "") err = printer.PrintResults(sample3)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -171,31 +174,34 @@ func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) {
elNode := el.Value.(*CandidateNode) elNode := el.Value.(*CandidateNode)
elNode.Document = 0 elNode.Document = 0
elNode.FileIndex = 0 elNode.FileIndex = 0
elNode.Node.HeadComment = "$yqLeadingContent$\n# go cats\n$yqDocSeperator$\n"
sample1 := nodeToList(elNode) sample1 := nodeToList(elNode)
el = el.Next() el = el.Next()
elNode = el.Value.(*CandidateNode) elNode = el.Value.(*CandidateNode)
elNode.Document = 0 elNode.Document = 0
elNode.FileIndex = 1 elNode.FileIndex = 1
elNode.Node.HeadComment = "$yqLeadingContent$\n$yqDocSeperator$\n"
sample2 := nodeToList(elNode) sample2 := nodeToList(elNode)
el = el.Next() el = el.Next()
elNode = el.Value.(*CandidateNode) elNode = el.Value.(*CandidateNode)
elNode.Document = 0 elNode.Document = 0
elNode.FileIndex = 2 elNode.FileIndex = 2
elNode.Node.HeadComment = "$yqLeadingContent$\n$yqDocSeperator$\n# cool\n"
sample3 := nodeToList(elNode) sample3 := nodeToList(elNode)
err = printer.PrintResults(sample1, "# go cats\n---\n") err = printer.PrintResults(sample1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample2, "---\n") err = printer.PrintResults(sample2)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample3, "---\n# cool\n") err = printer.PrintResults(sample3)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -214,7 +220,7 @@ func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
panic(err) panic(err)
} }
err = printer.PrintResults(inputs, "") err = printer.PrintResults(inputs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -233,7 +239,9 @@ func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) {
panic(err) panic(err)
} }
err = printer.PrintResults(inputs, "# go cats\n---\n") inputs.Front().Value.(*CandidateNode).Node.HeadComment = "$yqLeadingContent$\n# go cats\n$yqDocSeperator$\n"
err = printer.PrintResults(inputs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -259,8 +267,8 @@ func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
inputs.Front().Value.(*CandidateNode).Node.HeadComment = "$yqLeadingContent$\n$yqDocSeperator$\n"
err = printer.PrintResults(inputs, "---\n") err = printer.PrintResults(inputs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -313,7 +321,9 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
panic(err) panic(err)
} }
err = printer.PrintResults(inputs, "# ignore this") inputs.Front().Value.(*CandidateNode).Node.HeadComment = "$yqLeadingContent$\n# ignore this\n"
err = printer.PrintResults(inputs)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -35,7 +35,7 @@ func (s *streamEvaluator) EvaluateNew(expression string, printer Printer, leadin
candidateNode := &CandidateNode{ candidateNode := &CandidateNode{
Document: 0, Document: 0,
Filename: "", Filename: "",
Node: &yaml.Node{Tag: "!!null", Kind: yaml.ScalarNode}, Node: &yaml.Node{Kind: yaml.DocumentNode, HeadComment: leadingContent, Content: []*yaml.Node{{Tag: "!!null", Kind: yaml.ScalarNode}}},
FileIndex: 0, FileIndex: 0,
} }
inputList := list.New() inputList := list.New()
@ -45,7 +45,7 @@ func (s *streamEvaluator) EvaluateNew(expression string, printer Printer, leadin
if errorParsing != nil { if errorParsing != nil {
return errorParsing return errorParsing
} }
return printer.PrintResults(result.MatchingNodes, leadingContent) return printer.PrintResults(result.MatchingNodes)
} }
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer, leadingContentPreProcessing bool) error { func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer, leadingContentPreProcessing bool) error {
@ -100,6 +100,9 @@ func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *Expr
} else if errorReading != nil { } else if errorReading != nil {
return currentIndex, errorReading return currentIndex, errorReading
} }
if currentIndex == 0 {
dataBucket.HeadComment = leadingContent
}
candidateNode := &CandidateNode{ candidateNode := &CandidateNode{
Document: currentIndex, Document: currentIndex,
Filename: filename, Filename: filename,
@ -113,12 +116,7 @@ func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *Expr
if errorParsing != nil { if errorParsing != nil {
return currentIndex, errorParsing return currentIndex, errorParsing
} }
var err error err := printer.PrintResults(result.MatchingNodes)
if currentIndex == 0 {
err = printer.PrintResults(result.MatchingNodes, leadingContent)
} else {
err = printer.PrintResults(result.MatchingNodes, "")
}
if err != nil { if err != nil {
return currentIndex, err return currentIndex, err

View File

@ -12,7 +12,6 @@ import (
) )
func readStream(filename string, leadingContentPreProcessing bool) (io.Reader, string, error) { func readStream(filename string, leadingContentPreProcessing bool) (io.Reader, string, error) {
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
var reader *bufio.Reader var reader *bufio.Reader
if filename == "-" { if filename == "-" {
reader = bufio.NewReader(os.Stdin) reader = bufio.NewReader(os.Stdin)
@ -25,21 +24,33 @@ func readStream(filename string, leadingContentPreProcessing bool) (io.Reader, s
} }
reader = bufio.NewReader(file) reader = bufio.NewReader(file)
} }
var sb strings.Builder
if !leadingContentPreProcessing { if !leadingContentPreProcessing {
return reader, "", nil return reader, "", nil
} }
return processReadStream(reader)
}
func processReadStream(reader *bufio.Reader) (io.Reader, string, error) {
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
var sb strings.Builder
sb.WriteString("$yqLeadingContent$\n")
for { for {
peekBytes, err := reader.Peek(3) peekBytes, err := reader.Peek(3)
if err == io.EOF { if err == io.EOF {
// EOF are handled else where.. // EOF are handled else where..
return reader, sb.String(), nil return reader, sb.String(), nil
} else if err != nil { } else if err != nil {
return reader, sb.String(), err return reader, sb.String(), err
} else if string(peekBytes) == "---" || commentLineRegEx.MatchString(string(peekBytes)) { } else if string(peekBytes) == "---" {
_, err := reader.ReadString('\n')
sb.WriteString("$yqDocSeperator$\n")
if 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') line, err := reader.ReadString('\n')
sb.WriteString(line) sb.WriteString(line)
if err == io.EOF { if err == io.EOF {
@ -51,7 +62,6 @@ func readStream(filename string, leadingContentPreProcessing bool) (io.Reader, s
return reader, sb.String(), nil return reader, sb.String(), nil
} }
} }
} }
func readDocuments(reader io.Reader, filename string, fileIndex int) (*list.List, error) { func readDocuments(reader io.Reader, filename string, fileIndex int) (*list.List, error) {