From 8ad8903e21a9a42dcbc1aa2a00e6e797d00e531d Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 30 Oct 2021 09:56:47 +1100 Subject: [PATCH] Split printer! --- .gitignore | 6 +- acceptance_tests/basic.sh | 2 +- acceptance_tests/empty.sh | 1 + acceptance_tests/front-matter.sh | 1 + acceptance_tests/leading-seperator.sh | 1 + acceptance_tests/output-format.sh | 4 + acceptance_tests/pretty-print.sh | 3 + acceptance_tests/split-printer.sh | 157 ++++++++++++++++++++++++++ cmd/root.go | 2 +- go.mod | 5 + pkg/yqlib/operators_test.go | 4 +- pkg/yqlib/printer.go | 19 +--- pkg/yqlib/printer_writer.go | 35 +++--- 13 files changed, 205 insertions(+), 35 deletions(-) create mode 100755 acceptance_tests/split-printer.sh diff --git a/.gitignore b/.gitignore index fac7e030..00b1ab74 100644 --- a/.gitignore +++ b/.gitignore @@ -38,8 +38,12 @@ parts/ prime/ .snapcraft/ yq*.snap + test.yml -test2.yml +test*.yml +0.yml +1.yml +2.yml # man page diff --git a/acceptance_tests/basic.sh b/acceptance_tests/basic.sh index f83f9085..dc9069ae 100755 --- a/acceptance_tests/basic.sh +++ b/acceptance_tests/basic.sh @@ -1,7 +1,7 @@ #!/bin/bash setUp() { - rm -f test.yml + rm test*.yml || true } testBasicEvalRoundTrip() { diff --git a/acceptance_tests/empty.sh b/acceptance_tests/empty.sh index d7f344df..7ffb813f 100755 --- a/acceptance_tests/empty.sh +++ b/acceptance_tests/empty.sh @@ -1,6 +1,7 @@ #!/bin/bash setUp() { + rm test*.yml || true cat >test.yml <test.yml <test.yml <test.yml <test.yml <test.yml <test.yml <test.yml <test.yml <test.yml <test.yml <test2.yml < 0 { - name = result.MatchingNodes.Front().Value.(*CandidateNode).Node.Value - } + indexVariableNode := yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: fmt.Sprintf("%v", sp.index)} + indexVariableCandidate := CandidateNode{Node: &indexVariableNode} + + context := Context{MatchingNodes: node.AsList()} + context.SetVariable("index", indexVariableCandidate.AsList()) + result, err := sp.treeNavigator.GetMatchingNodes(context, sp.nameExpression) + if err != nil { + return nil, err } - if name == "" { - name = fmt.Sprintf("%v.%v", index, sp.extension) - } else { - name = fmt.Sprintf("%v.%v", name, sp.extension) + if result.MatchingNodes.Len() > 0 { + name = result.MatchingNodes.Front().Value.(*CandidateNode).Node.Value } + name = fmt.Sprintf("%v.%v", name, sp.extension) f, err := os.Create(name) if err != nil { return nil, err } + sp.index = sp.index + 1 return bufio.NewWriter(f), nil