mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
handle multiple document streams
This commit is contained in:
parent
db60746e4e
commit
09a9e1e7f0
@ -22,6 +22,7 @@ type resultsPrinter struct {
|
||||
writer io.Writer
|
||||
firstTimePrinting bool
|
||||
previousDocIndex uint
|
||||
previousFileIndex int
|
||||
printedMatches bool
|
||||
}
|
||||
|
||||
@ -89,19 +90,20 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
||||
return nil
|
||||
}
|
||||
if p.firstTimePrinting {
|
||||
p.previousDocIndex = matchingNodes.Front().Value.(*CandidateNode).Document
|
||||
node := matchingNodes.Front().Value.(*CandidateNode)
|
||||
p.previousDocIndex = node.Document
|
||||
p.previousFileIndex = node.FileIndex
|
||||
p.firstTimePrinting = false
|
||||
}
|
||||
|
||||
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
||||
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)
|
||||
if (p.previousDocIndex != mappedDoc.Document) && p.printDocSeparators {
|
||||
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators {
|
||||
log.Debug("-- writing doc sep")
|
||||
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {
|
||||
|
@ -52,7 +52,53 @@ func TestPrinterMultipleDocsInSequence(t *testing.T) {
|
||||
|
||||
writer.Flush()
|
||||
test.AssertResult(t, multiDocSample, output.String())
|
||||
}
|
||||
|
||||
func TestPrinterMultipleFilesInSequence(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
var writer = bufio.NewWriter(&output)
|
||||
printer := NewPrinter(writer, false, true, false, 2, true)
|
||||
|
||||
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
el := inputs.Front()
|
||||
elNode := el.Value.(*CandidateNode)
|
||||
elNode.Document = 0
|
||||
elNode.FileIndex = 0
|
||||
sample1 := nodeToMap(elNode)
|
||||
|
||||
el = el.Next()
|
||||
elNode = el.Value.(*CandidateNode)
|
||||
elNode.Document = 0
|
||||
elNode.FileIndex = 1
|
||||
sample2 := nodeToMap(elNode)
|
||||
|
||||
el = el.Next()
|
||||
elNode = el.Value.(*CandidateNode)
|
||||
elNode.Document = 0
|
||||
elNode.FileIndex = 2
|
||||
sample3 := nodeToMap(elNode)
|
||||
|
||||
err = printer.PrintResults(sample1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = printer.PrintResults(sample2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = printer.PrintResults(sample3)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
writer.Flush()
|
||||
test.AssertResult(t, multiDocSample, output.String())
|
||||
}
|
||||
|
||||
func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user