Speed up multiply

This commit is contained in:
Mike Farah 2021-10-11 14:46:46 +11:00
parent 79bc1f95cb
commit ed4d888bfa
4 changed files with 31 additions and 7 deletions

View File

@ -357,7 +357,7 @@ yq eval '.foobar * .foobarList' sample.yml
will output
```yaml
c: foobarList_c
<<:
!!merge <<:
- *foo
- *bar
thing: foobar_thing

View File

@ -111,6 +111,7 @@ func multiplyWithPrefs() lex.Action {
if strings.Contains(options, "d") {
prefs.DeepMergeArrays = true
}
prefs.TraversePrefs.DontFollowAlias = true
op := &Operation{OperationType: multiplyOpType, Value: multiplyOpType.Type, StringValue: options, Preferences: prefs}
return &token{TokenType: operationToken, Operation: op}, nil
}

View File

@ -6,6 +6,7 @@ import (
"container/list"
"github.com/jinzhu/copier"
yaml "gopkg.in/yaml.v3"
)
@ -50,14 +51,21 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, contex
if lhs.Node.Kind == yaml.MappingNode && rhs.Node.Kind == yaml.MappingNode ||
(lhs.Node.Kind == yaml.SequenceNode && rhs.Node.Kind == yaml.SequenceNode) {
var newBlank = lhs.CreateChild(nil, newBlankNode)
log.Debugf("merge - merge lhs into blank")
var newThing, err = mergeObjects(d, context.WritableClone(), newBlank, lhs, multiplyPreferences{})
var newBlank = CandidateNode{}
err := copier.CopyWithOption(&newBlank, lhs, copier.Option{IgnoreEmpty: true, DeepCopy: true})
if err != nil {
return nil, err
}
return mergeObjects(d, context.WritableClone(), newThing, rhs, preferences)
newBlank.Node.HeadComment = newBlankNode.HeadComment
newBlank.Node.FootComment = newBlankNode.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)
} else if (lhs.Node.Tag == "!!int" || lhs.Node.Tag == "!!float") && (rhs.Node.Tag == "!!int" || rhs.Node.Tag == "!!float") {

View File

@ -60,6 +60,13 @@ var nodeWithFooter = `
a: apple
# footer`
var document = `
a: &cat {name: cat}
b: {name: dog}
c:
<<: *cat
`
var multiplyOperatorScenarios = []expressionScenario{
{
skipDoc: true,
@ -405,7 +412,15 @@ var multiplyOperatorScenarios = []expressionScenario{
document: mergeDocSample,
expression: `.foobar * .foobarList`,
expected: []string{
"D0, P[foobar], (!!map)::c: foobarList_c\n<<: [*foo, *bar]\nthing: foobar_thing\nb: foobarList_b\n",
"D0, P[foobar], (!!map)::c: foobarList_c\n!!merge <<: [*foo, *bar]\nthing: foobar_thing\nb: foobarList_b\n",
},
},
{
skipDoc: true,
document: document,
expression: `.b * .c`,
expected: []string{
"D0, P[b], (!!map)::{name: dog, <<: *cat}\n",
},
},
}