Fixed header preprocessing!

This commit is contained in:
Mike Farah 2021-11-14 10:51:18 +11:00
parent 11b6261e8b
commit 33871bf007
9 changed files with 42 additions and 45 deletions

View File

@ -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))
}
}

View File

@ -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)

View File

@ -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",
},

View File

@ -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",

View File

@ -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",
},
},
}

View File

@ -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",
},
},
{

View File

@ -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: ``,

View File

@ -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"

View File

@ -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 {