mirror of
https://github.com/mikefarah/yq.git
synced 2025-02-04 01:55:46 +00:00
Fixed union infinite loop #930
This commit is contained in:
parent
8941573c1a
commit
3543a2dbdc
@ -2,8 +2,10 @@ package yqlib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/jinzhu/copier"
|
"github.com/jinzhu/copier"
|
||||||
|
logging "gopkg.in/op/go-logging.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
@ -51,6 +53,14 @@ func (n *Context) ChildContext(results *list.List) Context {
|
|||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Context) ToString() string {
|
||||||
|
if !log.IsEnabledFor(logging.DEBUG) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
result := fmt.Sprintf("Context\nDontAutoCreate: %v\n", n.DontAutoCreate)
|
||||||
|
return result + NodesToString(n.MatchingNodes)
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Context) Clone() Context {
|
func (n *Context) Clone() Context {
|
||||||
clone := Context{}
|
clone := Context{}
|
||||||
err := copier.Copy(&clone, n)
|
err := copier.Copy(&clone, n)
|
||||||
|
@ -179,7 +179,7 @@ func NodesToString(collection *list.List) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
result := ""
|
result := fmt.Sprintf("%v results\n", collection.Len())
|
||||||
for el := collection.Front(); el != nil; el = el.Next() {
|
for el := collection.Front(); el != nil; el = el.Next() {
|
||||||
result = result + "\n" + NodeToString(el.Value.(*CandidateNode))
|
result = result + "\n" + NodeToString(el.Value.(*CandidateNode))
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,36 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
func unionOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
func unionOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
|
log.Debug("unionOperator")
|
||||||
|
log.Debug("context: %v", NodesToString(context.MatchingNodes))
|
||||||
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs)
|
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Context{}, err
|
return Context{}, err
|
||||||
}
|
}
|
||||||
|
log.Debug("lhs: %v", NodesToString(lhs.MatchingNodes))
|
||||||
|
log.Debug("rhs input: %v", NodesToString(context.MatchingNodes))
|
||||||
|
log.Debug("rhs: %v", expressionNode.Rhs.Operation.toString())
|
||||||
rhs, err := d.GetMatchingNodes(context, expressionNode.Rhs)
|
rhs, err := d.GetMatchingNodes(context, expressionNode.Rhs)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Context{}, err
|
return Context{}, err
|
||||||
}
|
}
|
||||||
for el := rhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
log.Debug("lhs: %v", lhs.ToString())
|
||||||
node := el.Value.(*CandidateNode)
|
log.Debug("rhs: %v", rhs.ToString())
|
||||||
lhs.MatchingNodes.PushBack(node)
|
|
||||||
|
// this can happen when both expressions modify the context
|
||||||
|
// instead of creating their own.
|
||||||
|
/// (.foo = "bar"), (.thing = "cat")
|
||||||
|
|
||||||
|
if rhs.MatchingNodes != lhs.MatchingNodes {
|
||||||
|
|
||||||
|
for el := rhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
||||||
|
node := el.Value.(*CandidateNode)
|
||||||
|
log.Debug("processing %v", NodeToString(node))
|
||||||
|
|
||||||
|
lhs.MatchingNodes.PushBack(node)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
log.Debug("all together: %v", lhs.ToString())
|
||||||
return lhs, nil
|
return lhs, nil
|
||||||
}
|
}
|
||||||
|
@ -5,20 +5,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var unionOperatorScenarios = []expressionScenario{
|
var unionOperatorScenarios = []expressionScenario{
|
||||||
// {
|
{
|
||||||
// skipDoc: true,
|
skipDoc: true,
|
||||||
// document: "{}",
|
document: "{}",
|
||||||
// expression: `(.a, .b.c) as $x`,
|
expression: `(.a, .b.c) as $x`,
|
||||||
// expected: []string{
|
expected: []string{
|
||||||
// "D0, P[], (doc)::{}\n",
|
"D0, P[], (doc)::{}\n",
|
||||||
// },
|
},
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// skipDoc: true,
|
skipDoc: true,
|
||||||
// document: "{}",
|
expression: `(.foo = "bar"), (.toe = "jam")`,
|
||||||
// expression: `(.a, .b.c)`,
|
expected: []string{
|
||||||
// expected: []string{},
|
"D0, P[], ()::foo: bar\ntoe: jam\n",
|
||||||
// },
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Combine scalars",
|
description: "Combine scalars",
|
||||||
expression: `1, true, "cat"`,
|
expression: `1, true, "cat"`,
|
||||||
|
Loading…
Reference in New Issue
Block a user