From 3bd5e746dac679564d60dc32e49c8577d0949e2e Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 30 Nov 2021 13:07:27 +1100 Subject: [PATCH] better optional traverse array hack --- pkg/yqlib/expression_postfix.go | 9 +++++---- pkg/yqlib/operator_traverse_path.go | 7 ++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/yqlib/expression_postfix.go b/pkg/yqlib/expression_postfix.go index 3087b913..46783509 100644 --- a/pkg/yqlib/expression_postfix.go +++ b/pkg/yqlib/expression_postfix.go @@ -71,15 +71,16 @@ func (p *expressionPostFixerImpl) ConvertToPostfix(infixTokens []*token) ([]*Ope log.Debugf("deleting open bracket from opstack") //and append a collect to the result + // hack - see if there's the optional traverse flag - // on the close op - move it to the collect op. + // on the close op - move it to the traverse array op // allows for .["cat"]? prefs := traversePreferences{} closeTokenMatch := string(currentToken.Match.Bytes) if closeTokenMatch[len(closeTokenMatch)-1:] == "?" { prefs.OptionalTraverse = true } - result = append(result, &Operation{OperationType: collectOperator, Preferences: prefs}) + result = append(result, &Operation{OperationType: collectOperator}) log.Debugf("put collect onto the result") if opener != openCollect { result = append(result, &Operation{OperationType: shortPipeOpType}) @@ -87,9 +88,9 @@ func (p *expressionPostFixerImpl) ConvertToPostfix(infixTokens []*token) ([]*Ope } //traverseArrayCollect is a sneaky op that needs to be included too - //when closing a [] + //when closing a ] if len(opStack) > 0 && opStack[len(opStack)-1].Operation != nil && opStack[len(opStack)-1].Operation.OperationType == traverseArrayOpType { - + opStack[len(opStack)-1].Operation.Preferences = prefs opStack, result = popOpToResult(opStack, result) } diff --git a/pkg/yqlib/operator_traverse_path.go b/pkg/yqlib/operator_traverse_path.go index 636ca3cc..69671591 100644 --- a/pkg/yqlib/operator_traverse_path.go +++ b/pkg/yqlib/operator_traverse_path.go @@ -96,11 +96,8 @@ func traverseArrayOperator(d *dataTreeNavigator, context Context, expressionNode } prefs := traversePreferences{} - // we could have .[blah]? - // and we sneakily put the traverse prefs on the RHS prefs - // dodgey :/ - if expressionNode.Rhs != nil && expressionNode.Rhs.Operation.Preferences != nil { - prefs = expressionNode.Rhs.Operation.Preferences.(traversePreferences) + if expressionNode.Operation.Preferences != nil { + prefs = expressionNode.Operation.Preferences.(traversePreferences) } var indicesToTraverse = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Content