mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-29 02:25:41 +00:00
Fixed header preprocessing!
This commit is contained in:
parent
11b6261e8b
commit
33871bf007
@ -35,6 +35,6 @@ func TestAllAtOnceEvaluateNodes(t *testing.T) {
|
|||||||
for _, tt := range evaluateNodesScenario {
|
for _, tt := range evaluateNodesScenario {
|
||||||
node := test.ParseData(tt.document)
|
node := test.ParseData(tt.document)
|
||||||
list, _ := evaluator.EvaluateNodes(tt.expression, &node)
|
list, _ := evaluator.EvaluateNodes(tt.expression, &node)
|
||||||
test.AssertResultComplex(t, tt.expected, resultsToString(list))
|
test.AssertResultComplex(t, tt.expected, resultsToString(t, list))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,29 +21,25 @@ func multiplyOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
|
|||||||
return crossFunction(d, context, expressionNode, multiply(expressionNode.Operation.Preferences.(multiplyPreferences)), false)
|
return crossFunction(d, context, expressionNode, multiply(expressionNode.Operation.Preferences.(multiplyPreferences)), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNewBlankNode(lhs *yaml.Node, rhs *yaml.Node) *yaml.Node {
|
func getComments(lhs *CandidateNode, rhs *CandidateNode) (leadingContent string, headComment string, footComment string) {
|
||||||
|
leadingContent = rhs.LeadingContent
|
||||||
blankNode := &yaml.Node{}
|
headComment = rhs.Node.HeadComment
|
||||||
|
footComment = rhs.Node.FootComment
|
||||||
if lhs.HeadComment != "" {
|
if lhs.Node.HeadComment != "" || lhs.LeadingContent != "" {
|
||||||
blankNode.HeadComment = lhs.HeadComment
|
headComment = lhs.Node.HeadComment
|
||||||
} else if rhs.HeadComment != "" {
|
leadingContent = lhs.LeadingContent
|
||||||
blankNode.HeadComment = rhs.HeadComment
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if lhs.FootComment != "" {
|
if lhs.Node.FootComment != "" {
|
||||||
blankNode.FootComment = lhs.FootComment
|
footComment = lhs.Node.FootComment
|
||||||
} else if rhs.FootComment != "" {
|
|
||||||
blankNode.FootComment = rhs.FootComment
|
|
||||||
}
|
}
|
||||||
|
return leadingContent, headComment, footComment
|
||||||
return blankNode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
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) {
|
return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
||||||
// need to do this before unWrapping the potential document node
|
// 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)
|
lhs.Node = unwrapDoc(lhs.Node)
|
||||||
rhs.Node = unwrapDoc(rhs.Node)
|
rhs.Node = unwrapDoc(rhs.Node)
|
||||||
log.Debugf("Multipling LHS: %v", lhs.Node.Tag)
|
log.Debugf("Multipling LHS: %v", lhs.Node.Tag)
|
||||||
@ -56,15 +52,10 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, contex
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
newBlank.Node.HeadComment = newBlankNode.HeadComment
|
newBlank.LeadingContent = leadingContent
|
||||||
newBlank.Node.FootComment = newBlankNode.FootComment
|
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)
|
return mergeObjects(d, context.WritableClone(), &newBlank, rhs, preferences)
|
||||||
} else if lhs.Node.Tag == "!!int" && rhs.Node.Tag == "!!int" {
|
} else if lhs.Node.Tag == "!!int" && rhs.Node.Tag == "!!int" {
|
||||||
return multiplyIntegers(lhs, rhs)
|
return multiplyIntegers(lhs, rhs)
|
||||||
|
@ -40,8 +40,9 @@ var docWithHeader = `# here
|
|||||||
a: apple
|
a: apple
|
||||||
`
|
`
|
||||||
|
|
||||||
var nodeWithHeader = `# here
|
var nodeWithHeader = `node:
|
||||||
a: apple
|
# here
|
||||||
|
a: apple
|
||||||
`
|
`
|
||||||
|
|
||||||
var docNoComments = `b: banana
|
var docNoComments = `b: banana
|
||||||
@ -83,9 +84,9 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
document: nodeWithHeader,
|
document: nodeWithHeader,
|
||||||
document2: docNoComments,
|
document2: docNoComments,
|
||||||
expression: `select(fi == 0) * select(fi == 1)`,
|
expression: `(select(fi == 0) | .node) * select(fi == 1)`,
|
||||||
expected: []string{
|
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,
|
skipDoc: true,
|
||||||
document: docNoComments,
|
document: docNoComments,
|
||||||
document2: nodeWithHeader,
|
document2: nodeWithHeader,
|
||||||
expression: `select(fi == 0) * select(fi == 1)`,
|
expression: `select(fi == 0) * (select(fi == 1) | .node)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!map)::b: banana\n# here\na: apple\n",
|
"D0, P[], (!!map)::b: banana\n# here\na: apple\n",
|
||||||
},
|
},
|
||||||
|
@ -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], (!!map)::b: foobarList_b\n!!merge <<: [*foo, *bar]\nc: foobarList_c\n",
|
||||||
"D0, P[foobarList b], (!!str)::b\n",
|
"D0, P[foobarList b], (!!str)::b\n",
|
||||||
"D0, P[foobarList b], (!!str)::foobarList_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 <<], (!!seq)::[*foo, *bar]\n",
|
||||||
"D0, P[foobarList << 0], (alias)::*foo\n",
|
"D0, P[foobarList << 0], (alias)::*foo\n",
|
||||||
"D0, P[foobarList << 1], (alias)::*bar\n",
|
"D0, P[foobarList << 1], (alias)::*bar\n",
|
||||||
|
@ -142,8 +142,8 @@ e: >-
|
|||||||
document: `a: cat`,
|
document: `a: cat`,
|
||||||
expression: `.. | style`,
|
expression: `.. | style`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!str)::\"\"\n",
|
"D0, P[], (!!str)::\n",
|
||||||
"D0, P[a], (!!str)::\"\"\n",
|
"D0, P[a], (!!str)::\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,12 @@ var tagOperatorScenarios = []expressionScenario{
|
|||||||
document: `{a: cat, b: 5, c: 3.2, e: true, f: []}`,
|
document: `{a: cat, b: 5, c: 3.2, e: true, f: []}`,
|
||||||
expression: `.. | tag`,
|
expression: `.. | tag`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!str)::'!!map'\n",
|
"D0, P[], (!!str)::!!map\n",
|
||||||
"D0, P[a], (!!str)::'!!str'\n",
|
"D0, P[a], (!!str)::!!str\n",
|
||||||
"D0, P[b], (!!str)::'!!int'\n",
|
"D0, P[b], (!!str)::!!int\n",
|
||||||
"D0, P[c], (!!str)::'!!float'\n",
|
"D0, P[c], (!!str)::!!float\n",
|
||||||
"D0, P[e], (!!str)::'!!bool'\n",
|
"D0, P[e], (!!str)::!!bool\n",
|
||||||
"D0, P[f], (!!str)::'!!seq'\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: []}`,
|
document: `{a: cat, b: 5, c: 3.2, e: true, f: []}`,
|
||||||
expression: `tag`,
|
expression: `tag`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!str)::'!!map'\n",
|
"D0, P[], (!!str)::!!map\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -125,13 +125,13 @@ var valueOperatorScenarios = []expressionScenario{
|
|||||||
document: ``,
|
document: ``,
|
||||||
expression: `"1.3"`,
|
expression: `"1.3"`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!str)::\"1.3\"\n",
|
"D0, P[], (!!str)::1.3\n",
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
document: ``,
|
document: ``,
|
||||||
expression: `"true"`,
|
expression: `"true"`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!str)::\"true\"\n",
|
"D0, P[], (!!str)::true\n",
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
document: ``,
|
document: ``,
|
||||||
|
@ -87,17 +87,23 @@ func testScenario(t *testing.T, s *expressionScenario) {
|
|||||||
t.Error(fmt.Errorf("%v: %v", err, s.expression))
|
t.Error(fmt.Errorf("%v: %v", err, s.expression))
|
||||||
return
|
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)
|
var pretty []string = make([]string, 0)
|
||||||
|
|
||||||
for el := results.Front(); el != nil; el = el.Next() {
|
for el := results.Front(); el != nil; el = el.Next() {
|
||||||
n := el.Value.(*CandidateNode)
|
n := el.Value.(*CandidateNode)
|
||||||
var valueBuffer bytes.Buffer
|
var valueBuffer bytes.Buffer
|
||||||
printer := NewPrinterWithSingleWriter(bufio.NewWriter(&valueBuffer), YamlOutputFormat, true, false, 4, true)
|
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
|
tag := n.Node.Tag
|
||||||
if n.Node.Kind == yaml.DocumentNode {
|
if n.Node.Kind == yaml.DocumentNode {
|
||||||
tag = "doc"
|
tag = "doc"
|
||||||
|
@ -40,7 +40,6 @@ func processLeadingContent(mappedDoc *CandidateNode, writer io.Writer, printDocS
|
|||||||
log.Debug("headcommentwas %v", mappedDoc.LeadingContent)
|
log.Debug("headcommentwas %v", mappedDoc.LeadingContent)
|
||||||
log.Debug("finished headcomment")
|
log.Debug("finished headcomment")
|
||||||
reader := bufio.NewReader(strings.NewReader(mappedDoc.LeadingContent))
|
reader := bufio.NewReader(strings.NewReader(mappedDoc.LeadingContent))
|
||||||
mappedDoc.Node.HeadComment = ""
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user