diff --git a/pkg/yqlib/all_at_once_evaluator_test.go b/pkg/yqlib/all_at_once_evaluator_test.go index b75ab300..2e9abc43 100644 --- a/pkg/yqlib/all_at_once_evaluator_test.go +++ b/pkg/yqlib/all_at_once_evaluator_test.go @@ -35,6 +35,6 @@ func TestAllAtOnceEvaluateNodes(t *testing.T) { for _, tt := range evaluateNodesScenario { node := test.ParseData(tt.document) list, _ := evaluator.EvaluateNodes(tt.expression, &node) - test.AssertResultComplex(t, tt.expected, resultsToString(list)) + test.AssertResultComplex(t, tt.expected, resultsToString(t, list)) } } diff --git a/pkg/yqlib/operator_multiply.go b/pkg/yqlib/operator_multiply.go index 00265387..2746b568 100644 --- a/pkg/yqlib/operator_multiply.go +++ b/pkg/yqlib/operator_multiply.go @@ -21,29 +21,25 @@ func multiplyOperator(d *dataTreeNavigator, context Context, expressionNode *Exp return crossFunction(d, context, expressionNode, multiply(expressionNode.Operation.Preferences.(multiplyPreferences)), false) } -func getNewBlankNode(lhs *yaml.Node, rhs *yaml.Node) *yaml.Node { - - blankNode := &yaml.Node{} - - if lhs.HeadComment != "" { - blankNode.HeadComment = lhs.HeadComment - } else if rhs.HeadComment != "" { - blankNode.HeadComment = rhs.HeadComment +func getComments(lhs *CandidateNode, rhs *CandidateNode) (leadingContent string, headComment string, footComment string) { + leadingContent = rhs.LeadingContent + headComment = rhs.Node.HeadComment + footComment = rhs.Node.FootComment + if lhs.Node.HeadComment != "" || lhs.LeadingContent != "" { + headComment = lhs.Node.HeadComment + leadingContent = lhs.LeadingContent } - if lhs.FootComment != "" { - blankNode.FootComment = lhs.FootComment - } else if rhs.FootComment != "" { - blankNode.FootComment = rhs.FootComment + if lhs.Node.FootComment != "" { + footComment = lhs.Node.FootComment } - - return blankNode + return leadingContent, headComment, footComment } func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { // need to do this before unWrapping the potential document node - newBlankNode := getNewBlankNode(lhs.Node, rhs.Node) + leadingContent, headComment, footComment := getComments(lhs, rhs) lhs.Node = unwrapDoc(lhs.Node) rhs.Node = unwrapDoc(rhs.Node) log.Debugf("Multipling LHS: %v", lhs.Node.Tag) @@ -56,15 +52,10 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, contex if err != nil { return nil, err } - newBlank.Node.HeadComment = newBlankNode.HeadComment - newBlank.Node.FootComment = newBlankNode.FootComment + newBlank.LeadingContent = leadingContent + newBlank.Node.HeadComment = headComment + newBlank.Node.FootComment = footComment - // var newBlank = lhs.CreateChild(nil, newBlankNode) - // log.Debugf("merge - merge lhs into blank") - // var newThing, err = mergeObjects(d, context.WritableClone(), newBlank, lhs, multiplyPreferences{}) - // if err != nil { - // return nil, err - // } return mergeObjects(d, context.WritableClone(), &newBlank, rhs, preferences) } else if lhs.Node.Tag == "!!int" && rhs.Node.Tag == "!!int" { return multiplyIntegers(lhs, rhs) diff --git a/pkg/yqlib/operator_multiply_test.go b/pkg/yqlib/operator_multiply_test.go index 7e1f38f2..b1355075 100644 --- a/pkg/yqlib/operator_multiply_test.go +++ b/pkg/yqlib/operator_multiply_test.go @@ -40,8 +40,9 @@ var docWithHeader = `# here a: apple ` -var nodeWithHeader = `# here -a: apple +var nodeWithHeader = `node: + # here + a: apple ` var docNoComments = `b: banana @@ -83,9 +84,9 @@ var multiplyOperatorScenarios = []expressionScenario{ skipDoc: true, document: nodeWithHeader, document2: docNoComments, - expression: `select(fi == 0) * select(fi == 1)`, + expression: `(select(fi == 0) | .node) * select(fi == 1)`, expected: []string{ - "D0, P[], (!!map)::# here\na: apple\nb: banana\n", + "D0, P[node], (!!map)::# here\na: apple\nb: banana\n", }, }, { @@ -101,7 +102,7 @@ var multiplyOperatorScenarios = []expressionScenario{ skipDoc: true, document: docNoComments, document2: nodeWithHeader, - expression: `select(fi == 0) * select(fi == 1)`, + expression: `select(fi == 0) * (select(fi == 1) | .node)`, expected: []string{ "D0, P[], (!!map)::b: banana\n# here\na: apple\n", }, diff --git a/pkg/yqlib/operator_recursive_descent_test.go b/pkg/yqlib/operator_recursive_descent_test.go index 61caab12..0893242b 100644 --- a/pkg/yqlib/operator_recursive_descent_test.go +++ b/pkg/yqlib/operator_recursive_descent_test.go @@ -219,7 +219,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{ "D0, P[foobarList], (!!map)::b: foobarList_b\n!!merge <<: [*foo, *bar]\nc: foobarList_c\n", "D0, P[foobarList b], (!!str)::b\n", "D0, P[foobarList b], (!!str)::foobarList_b\n", - "D0, P[foobarList <<], (!!merge)::!!merge <<\n", + "D0, P[foobarList <<], (!!merge)::<<\n", "D0, P[foobarList <<], (!!seq)::[*foo, *bar]\n", "D0, P[foobarList << 0], (alias)::*foo\n", "D0, P[foobarList << 1], (alias)::*bar\n", diff --git a/pkg/yqlib/operator_style_test.go b/pkg/yqlib/operator_style_test.go index e02fa3ad..5f2490fd 100644 --- a/pkg/yqlib/operator_style_test.go +++ b/pkg/yqlib/operator_style_test.go @@ -142,8 +142,8 @@ e: >- document: `a: cat`, expression: `.. | style`, expected: []string{ - "D0, P[], (!!str)::\"\"\n", - "D0, P[a], (!!str)::\"\"\n", + "D0, P[], (!!str)::\n", + "D0, P[a], (!!str)::\n", }, }, } diff --git a/pkg/yqlib/operator_tag_test.go b/pkg/yqlib/operator_tag_test.go index fd90f734..08b22741 100644 --- a/pkg/yqlib/operator_tag_test.go +++ b/pkg/yqlib/operator_tag_test.go @@ -10,12 +10,12 @@ var tagOperatorScenarios = []expressionScenario{ document: `{a: cat, b: 5, c: 3.2, e: true, f: []}`, expression: `.. | tag`, expected: []string{ - "D0, P[], (!!str)::'!!map'\n", - "D0, P[a], (!!str)::'!!str'\n", - "D0, P[b], (!!str)::'!!int'\n", - "D0, P[c], (!!str)::'!!float'\n", - "D0, P[e], (!!str)::'!!bool'\n", - "D0, P[f], (!!str)::'!!seq'\n", + "D0, P[], (!!str)::!!map\n", + "D0, P[a], (!!str)::!!str\n", + "D0, P[b], (!!str)::!!int\n", + "D0, P[c], (!!str)::!!float\n", + "D0, P[e], (!!str)::!!bool\n", + "D0, P[f], (!!str)::!!seq\n", }, }, { @@ -23,7 +23,7 @@ var tagOperatorScenarios = []expressionScenario{ document: `{a: cat, b: 5, c: 3.2, e: true, f: []}`, expression: `tag`, expected: []string{ - "D0, P[], (!!str)::'!!map'\n", + "D0, P[], (!!str)::!!map\n", }, }, { diff --git a/pkg/yqlib/operator_value_test.go b/pkg/yqlib/operator_value_test.go index 4821c1e3..81534804 100644 --- a/pkg/yqlib/operator_value_test.go +++ b/pkg/yqlib/operator_value_test.go @@ -125,13 +125,13 @@ var valueOperatorScenarios = []expressionScenario{ document: ``, expression: `"1.3"`, expected: []string{ - "D0, P[], (!!str)::\"1.3\"\n", + "D0, P[], (!!str)::1.3\n", }, }, { document: ``, expression: `"true"`, expected: []string{ - "D0, P[], (!!str)::\"true\"\n", + "D0, P[], (!!str)::true\n", }, }, { document: ``, diff --git a/pkg/yqlib/operators_test.go b/pkg/yqlib/operators_test.go index 10c9260d..9352950a 100644 --- a/pkg/yqlib/operators_test.go +++ b/pkg/yqlib/operators_test.go @@ -87,17 +87,23 @@ func testScenario(t *testing.T, s *expressionScenario) { t.Error(fmt.Errorf("%v: %v", err, s.expression)) return } - test.AssertResultComplexWithContext(t, s.expected, resultsToString(context.MatchingNodes), fmt.Sprintf("desc: %v\nexp: %v\ndoc: %v", s.description, s.expression, s.document)) + test.AssertResultComplexWithContext(t, s.expected, resultsToString(t, context.MatchingNodes), fmt.Sprintf("desc: %v\nexp: %v\ndoc: %v", s.description, s.expression, s.document)) } -func resultsToString(results *list.List) []string { +func resultsToString(t *testing.T, results *list.List) []string { var pretty []string = make([]string, 0) for el := results.Front(); el != nil; el = el.Next() { n := el.Value.(*CandidateNode) var valueBuffer bytes.Buffer printer := NewPrinterWithSingleWriter(bufio.NewWriter(&valueBuffer), YamlOutputFormat, true, false, 4, true) - printer.PrintResults(n.AsList()) + + err := printer.PrintResults(n.AsList()) + if err != nil { + t.Error(err) + return nil + } + tag := n.Node.Tag if n.Node.Kind == yaml.DocumentNode { tag = "doc" diff --git a/pkg/yqlib/utils.go b/pkg/yqlib/utils.go index 7405f730..5d46a29e 100644 --- a/pkg/yqlib/utils.go +++ b/pkg/yqlib/utils.go @@ -40,7 +40,6 @@ func processLeadingContent(mappedDoc *CandidateNode, writer io.Writer, printDocS log.Debug("headcommentwas %v", mappedDoc.LeadingContent) log.Debug("finished headcomment") reader := bufio.NewReader(strings.NewReader(mappedDoc.LeadingContent)) - mappedDoc.Node.HeadComment = "" for {