mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	Renaming pathtree to expression
This commit is contained in:
		
							parent
							
								
									7d5b6b5442
								
							
						
					
					
						commit
						e86f83fb69
					
				| @ -19,11 +19,11 @@ type Evaluator interface { | ||||
| 
 | ||||
| type allAtOnceEvaluator struct { | ||||
| 	treeNavigator DataTreeNavigator | ||||
| 	treeCreator   PathTreeCreator | ||||
| 	treeCreator   ExpressionParser | ||||
| } | ||||
| 
 | ||||
| func NewAllAtOnceEvaluator() Evaluator { | ||||
| 	return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewPathTreeCreator()} | ||||
| 	return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewExpressionParser()} | ||||
| } | ||||
| 
 | ||||
| func (e *allAtOnceEvaluator) EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) { | ||||
| @ -35,7 +35,7 @@ func (e *allAtOnceEvaluator) EvaluateNodes(expression string, nodes ...*yaml.Nod | ||||
| } | ||||
| 
 | ||||
| func (e *allAtOnceEvaluator) EvaluateCandidateNodes(expression string, inputCandidates *list.List) (*list.List, error) { | ||||
| 	node, err := e.treeCreator.ParsePath(expression) | ||||
| 	node, err := e.treeCreator.ParseExpression(expression) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| @ -9,10 +9,10 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| type DataTreeNavigator interface { | ||||
| 	// given a list of CandidateEntities and a pathNode, | ||||
| 	// this will process the list against the given pathNode and return | ||||
| 	// given a list of CandidateEntities and a expressionNode, | ||||
| 	// this will process the list against the given expressionNode and return | ||||
| 	// a new list of matching candidates | ||||
| 	GetMatchingNodes(matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) | ||||
| 	GetMatchingNodes(matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) | ||||
| } | ||||
| 
 | ||||
| type dataTreeNavigator struct { | ||||
| @ -22,22 +22,22 @@ func NewDataTreeNavigator() DataTreeNavigator { | ||||
| 	return &dataTreeNavigator{} | ||||
| } | ||||
| 
 | ||||
| func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	if pathNode == nil { | ||||
| func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	if expressionNode == nil { | ||||
| 		log.Debugf("getMatchingNodes - nothing to do") | ||||
| 		return matchingNodes, nil | ||||
| 	} | ||||
| 	log.Debugf("Processing Op: %v", pathNode.Operation.toString()) | ||||
| 	log.Debugf("Processing Op: %v", expressionNode.Operation.toString()) | ||||
| 	if log.IsEnabledFor(logging.DEBUG) { | ||||
| 		for el := matchingNodes.Front(); el != nil; el = el.Next() { | ||||
| 			log.Debug(NodeToString(el.Value.(*CandidateNode))) | ||||
| 		} | ||||
| 	} | ||||
| 	log.Debug(">>") | ||||
| 	handler := pathNode.Operation.OperationType.Handler | ||||
| 	handler := expressionNode.Operation.OperationType.Handler | ||||
| 	if handler != nil { | ||||
| 		return handler(d, matchingNodes, pathNode) | ||||
| 		return handler(d, matchingNodes, expressionNode) | ||||
| 	} | ||||
| 	return nil, fmt.Errorf("Unknown operator %v", pathNode.Operation.OperationType) | ||||
| 	return nil, fmt.Errorf("Unknown operator %v", expressionNode.Operation.OperationType) | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -5,29 +5,28 @@ import ( | ||||
| 	"strings" | ||||
| ) | ||||
| 
 | ||||
| var myPathTokeniser = newPathTokeniser() | ||||
| var myPathPostfixer = newPathPostFixer() | ||||
| var myPathTokeniser = newExpressionTokeniser() | ||||
| var myPathPostfixer = newExpressionPostFixer() | ||||
| 
 | ||||
| type PathTreeNode struct { | ||||
| type ExpressionNode struct { | ||||
| 	Operation *Operation | ||||
| 	Lhs       *PathTreeNode | ||||
| 	Rhs       *PathTreeNode | ||||
| 	Lhs       *ExpressionNode | ||||
| 	Rhs       *ExpressionNode | ||||
| } | ||||
| 
 | ||||
| type PathTreeCreator interface { | ||||
| 	ParsePath(path string) (*PathTreeNode, error) | ||||
| 	CreatePathTree(postFixPath []*Operation) (*PathTreeNode, error) | ||||
| type ExpressionParser interface { | ||||
| 	ParseExpression(expression string) (*ExpressionNode, error) | ||||
| } | ||||
| 
 | ||||
| type pathTreeCreator struct { | ||||
| type expressionParserImpl struct { | ||||
| } | ||||
| 
 | ||||
| func NewPathTreeCreator() PathTreeCreator { | ||||
| 	return &pathTreeCreator{} | ||||
| func NewExpressionParser() ExpressionParser { | ||||
| 	return &expressionParserImpl{} | ||||
| } | ||||
| 
 | ||||
| func (p *pathTreeCreator) ParsePath(path string) (*PathTreeNode, error) { | ||||
| 	tokens, err := myPathTokeniser.Tokenise(path) | ||||
| func (p *expressionParserImpl) ParseExpression(expression string) (*ExpressionNode, error) { | ||||
| 	tokens, err := myPathTokeniser.Tokenise(expression) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @ -36,18 +35,18 @@ func (p *pathTreeCreator) ParsePath(path string) (*PathTreeNode, error) { | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return p.CreatePathTree(Operations) | ||||
| 	return p.createExpressionTree(Operations) | ||||
| } | ||||
| 
 | ||||
| func (p *pathTreeCreator) CreatePathTree(postFixPath []*Operation) (*PathTreeNode, error) { | ||||
| 	var stack = make([]*PathTreeNode, 0) | ||||
| func (p *expressionParserImpl) createExpressionTree(postFixPath []*Operation) (*ExpressionNode, error) { | ||||
| 	var stack = make([]*ExpressionNode, 0) | ||||
| 
 | ||||
| 	if len(postFixPath) == 0 { | ||||
| 		return nil, nil | ||||
| 	} | ||||
| 
 | ||||
| 	for _, Operation := range postFixPath { | ||||
| 		var newNode = PathTreeNode{Operation: Operation} | ||||
| 		var newNode = ExpressionNode{Operation: Operation} | ||||
| 		log.Debugf("pathTree %v ", Operation.toString()) | ||||
| 		if Operation.OperationType.NumArgs > 0 { | ||||
| 			numArgs := Operation.OperationType.NumArgs | ||||
| @ -7,36 +7,36 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| func TestPathTreeNoArgsForTwoArgOp(t *testing.T) { | ||||
| 	_, err := NewPathTreeCreator().ParsePath("=") | ||||
| 	_, err := NewExpressionParser().ParseExpression("=") | ||||
| 	test.AssertResultComplex(t, "'=' expects 2 args but there is 0", err.Error()) | ||||
| } | ||||
| 
 | ||||
| func TestPathTreeOneLhsArgsForTwoArgOp(t *testing.T) { | ||||
| 	_, err := NewPathTreeCreator().ParsePath(".a =") | ||||
| 	_, err := NewExpressionParser().ParseExpression(".a =") | ||||
| 	test.AssertResultComplex(t, "'=' expects 2 args but there is 1", err.Error()) | ||||
| } | ||||
| 
 | ||||
| func TestPathTreeOneRhsArgsForTwoArgOp(t *testing.T) { | ||||
| 	_, err := NewPathTreeCreator().ParsePath("= .a") | ||||
| 	_, err := NewExpressionParser().ParseExpression("= .a") | ||||
| 	test.AssertResultComplex(t, "'=' expects 2 args but there is 1", err.Error()) | ||||
| } | ||||
| 
 | ||||
| func TestPathTreeTwoArgsForTwoArgOp(t *testing.T) { | ||||
| 	_, err := NewPathTreeCreator().ParsePath(".a = .b") | ||||
| 	_, err := NewExpressionParser().ParseExpression(".a = .b") | ||||
| 	test.AssertResultComplex(t, nil, err) | ||||
| } | ||||
| 
 | ||||
| func TestPathTreeNoArgsForOneArgOp(t *testing.T) { | ||||
| 	_, err := NewPathTreeCreator().ParsePath("explode") | ||||
| 	_, err := NewExpressionParser().ParseExpression("explode") | ||||
| 	test.AssertResultComplex(t, "'explode' expects 1 arg but received none", err.Error()) | ||||
| } | ||||
| 
 | ||||
| func TestPathTreeOneArgForOneArgOp(t *testing.T) { | ||||
| 	_, err := NewPathTreeCreator().ParsePath("explode(.)") | ||||
| 	_, err := NewExpressionParser().ParseExpression("explode(.)") | ||||
| 	test.AssertResultComplex(t, nil, err) | ||||
| } | ||||
| 
 | ||||
| func TestPathTreeExtraArgs(t *testing.T) { | ||||
| 	_, err := NewPathTreeCreator().ParsePath("sortKeys(.) explode(.)") | ||||
| 	_, err := NewExpressionParser().ParseExpression("sortKeys(.) explode(.)") | ||||
| 	test.AssertResultComplex(t, "expected end of expression but found 'explode', please check expression syntax", err.Error()) | ||||
| } | ||||
| @ -6,15 +6,15 @@ import ( | ||||
| 	logging "gopkg.in/op/go-logging.v1" | ||||
| ) | ||||
| 
 | ||||
| type pathPostFixer interface { | ||||
| type expressionPostFixer interface { | ||||
| 	ConvertToPostfix([]*token) ([]*Operation, error) | ||||
| } | ||||
| 
 | ||||
| type pathPostFixerImpl struct { | ||||
| type expressionPostFixerImpl struct { | ||||
| } | ||||
| 
 | ||||
| func newPathPostFixer() pathPostFixer { | ||||
| 	return &pathPostFixerImpl{} | ||||
| func newExpressionPostFixer() expressionPostFixer { | ||||
| 	return &expressionPostFixerImpl{} | ||||
| } | ||||
| 
 | ||||
| func popOpToResult(opStack []*token, result []*Operation) ([]*token, []*Operation) { | ||||
| @ -23,7 +23,7 @@ func popOpToResult(opStack []*token, result []*Operation) ([]*token, []*Operatio | ||||
| 	return opStack, append(result, newOp.Operation) | ||||
| } | ||||
| 
 | ||||
| func (p *pathPostFixerImpl) ConvertToPostfix(infixTokens []*token) ([]*Operation, error) { | ||||
| func (p *expressionPostFixerImpl) ConvertToPostfix(infixTokens []*token) ([]*Operation, error) { | ||||
| 	var result []*Operation | ||||
| 	// surround the whole thing with quotes | ||||
| 	var opStack = []*token{&token{TokenType: openBracket}} | ||||
| @ -164,8 +164,8 @@ var pathTests = []struct { | ||||
| 	}, | ||||
| } | ||||
| 
 | ||||
| var tokeniser = newPathTokeniser() | ||||
| var postFixer = newPathPostFixer() | ||||
| var tokeniser = newExpressionTokeniser() | ||||
| var postFixer = newExpressionPostFixer() | ||||
| 
 | ||||
| func TestPathParsing(t *testing.T) { | ||||
| 	for _, tt := range pathTests { | ||||
| @ -307,24 +307,24 @@ func initLexer() (*lex.Lexer, error) { | ||||
| 	return lexer, nil | ||||
| } | ||||
| 
 | ||||
| type pathTokeniser interface { | ||||
| 	Tokenise(path string) ([]*token, error) | ||||
| type expressionTokeniser interface { | ||||
| 	Tokenise(expression string) ([]*token, error) | ||||
| } | ||||
| 
 | ||||
| type pathTokeniserImpl struct { | ||||
| type expressionTokeniserImpl struct { | ||||
| 	lexer *lex.Lexer | ||||
| } | ||||
| 
 | ||||
| func newPathTokeniser() pathTokeniser { | ||||
| func newExpressionTokeniser() expressionTokeniser { | ||||
| 	var lexer, err = initLexer() | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	return &pathTokeniserImpl{lexer} | ||||
| 	return &expressionTokeniserImpl{lexer} | ||||
| } | ||||
| 
 | ||||
| func (p *pathTokeniserImpl) Tokenise(path string) ([]*token, error) { | ||||
| 	scanner, err := p.lexer.Scanner([]byte(path)) | ||||
| func (p *expressionTokeniserImpl) Tokenise(expression string) ([]*token, error) { | ||||
| 	scanner, err := p.lexer.Scanner([]byte(expression)) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("Parsing expression: %v", err) | ||||
| @ -356,7 +356,7 @@ func (p *pathTokeniserImpl) Tokenise(path string) ([]*token, error) { | ||||
| 	return postProcessedTokens, nil | ||||
| } | ||||
| 
 | ||||
| func (p *pathTokeniserImpl) handleToken(tokens []*token, index int, postProcessedTokens []*token) (tokensAccum []*token, skipNextToken bool) { | ||||
| func (p *expressionTokeniserImpl) handleToken(tokens []*token, index int, postProcessedTokens []*token) (tokensAccum []*token, skipNextToken bool) { | ||||
| 	skipNextToken = false | ||||
| 	currentToken := tokens[index] | ||||
| 
 | ||||
| @ -9,17 +9,17 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func createAddOp(lhs *PathTreeNode, rhs *PathTreeNode) *PathTreeNode { | ||||
| 	return &PathTreeNode{Operation: &Operation{OperationType: addOpType}, | ||||
| func createAddOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { | ||||
| 	return &ExpressionNode{Operation: &Operation{OperationType: addOpType}, | ||||
| 		Lhs: lhs, | ||||
| 		Rhs: rhs} | ||||
| } | ||||
| 
 | ||||
| func addAssignOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func addAssignOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	assignmentOp := &Operation{OperationType: assignOpType} | ||||
| 	assignmentOp.UpdateAssign = false | ||||
| 
 | ||||
| 	assignmentOpNode := &PathTreeNode{Operation: assignmentOp, Lhs: pathNode.Lhs, Rhs: createAddOp(pathNode.Lhs, pathNode.Rhs)} | ||||
| 	assignmentOpNode := &ExpressionNode{Operation: assignmentOp, Lhs: expressionNode.Lhs, Rhs: createAddOp(expressionNode.Lhs, expressionNode.Rhs)} | ||||
| 	return d.GetMatchingNodes(matchingNodes, assignmentOpNode) | ||||
| } | ||||
| 
 | ||||
| @ -37,10 +37,10 @@ func toNodes(candidate *CandidateNode) []*yaml.Node { | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| func addOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func addOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("Add operator") | ||||
| 
 | ||||
| 	return crossFunction(d, matchingNodes, pathNode, add) | ||||
| 	return crossFunction(d, matchingNodes, expressionNode, add) | ||||
| } | ||||
| 
 | ||||
| func add(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { | ||||
|  | ||||
| @ -7,9 +7,9 @@ import ( | ||||
| // corssFunction no matches | ||||
| // can boolean use crossfunction | ||||
| 
 | ||||
| func alternativeOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func alternativeOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- alternative") | ||||
| 	return crossFunction(d, matchingNodes, pathNode, alternativeFunc) | ||||
| 	return crossFunction(d, matchingNodes, expressionNode, alternativeFunc) | ||||
| } | ||||
| 
 | ||||
| func alternativeFunc(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { | ||||
|  | ||||
| @ -6,13 +6,13 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func assignAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func assignAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	log.Debugf("AssignAlias operator!") | ||||
| 
 | ||||
| 	aliasName := "" | ||||
| 	if !pathNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	if !expressionNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @ -21,7 +21,7 @@ func assignAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @ -31,8 +31,8 @@ func assignAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 		log.Debugf("Setting aliasName : %v", candidate.GetKey()) | ||||
| 
 | ||||
| 		if pathNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		if expressionNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| @ -47,7 +47,7 @@ func assignAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 	return matchingNodes, nil | ||||
| } | ||||
| 
 | ||||
| func getAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func getAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("GetAlias operator!") | ||||
| 	var results = list.New() | ||||
| 
 | ||||
| @ -60,13 +60,13 @@ func getAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode * | ||||
| 	return results, nil | ||||
| } | ||||
| 
 | ||||
| func assignAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func assignAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	log.Debugf("AssignAnchor operator!") | ||||
| 
 | ||||
| 	anchorName := "" | ||||
| 	if !pathNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	if !expressionNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @ -76,7 +76,7 @@ func assignAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNo | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @ -86,8 +86,8 @@ func assignAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNo | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 		log.Debugf("Setting anchorName of : %v", candidate.GetKey()) | ||||
| 
 | ||||
| 		if pathNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		if expressionNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| @ -102,7 +102,7 @@ func assignAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNo | ||||
| 	return matchingNodes, nil | ||||
| } | ||||
| 
 | ||||
| func getAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func getAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("GetAnchor operator!") | ||||
| 	var results = list.New() | ||||
| 
 | ||||
| @ -116,13 +116,13 @@ func getAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode | ||||
| 	return results, nil | ||||
| } | ||||
| 
 | ||||
| func explodeOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func explodeOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- ExplodeOperation") | ||||
| 
 | ||||
| 	for el := matchMap.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 
 | ||||
| 		rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
|  | ||||
| @ -2,21 +2,21 @@ package yqlib | ||||
| 
 | ||||
| import "container/list" | ||||
| 
 | ||||
| func assignUpdateOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| func assignUpdateOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	var rhs *list.List | ||||
| 	if !pathNode.Operation.UpdateAssign { | ||||
| 		rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	if !expressionNode.Operation.UpdateAssign { | ||||
| 		rhs, err = d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 	} | ||||
| 
 | ||||
| 	for el := lhs.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 
 | ||||
| 		if pathNode.Operation.UpdateAssign { | ||||
| 			rhs, err = d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		if expressionNode.Operation.UpdateAssign { | ||||
| 			rhs, err = d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 		} | ||||
| 
 | ||||
| 		if err != nil { | ||||
| @ -37,15 +37,15 @@ func assignUpdateOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNo | ||||
| } | ||||
| 
 | ||||
| // does not update content or values | ||||
| func assignAttributesOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| func assignAttributesOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	for el := lhs.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 
 | ||||
| 		rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
|  | ||||
| @ -44,23 +44,23 @@ func performBoolOp(op boolOp) func(d *dataTreeNavigator, lhs *CandidateNode, rhs | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func orOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func orOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- orOp") | ||||
| 	return crossFunction(d, matchingNodes, pathNode, performBoolOp( | ||||
| 	return crossFunction(d, matchingNodes, expressionNode, performBoolOp( | ||||
| 		func(b1 bool, b2 bool) bool { | ||||
| 			return b1 || b2 | ||||
| 		})) | ||||
| } | ||||
| 
 | ||||
| func andOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func andOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- AndOp") | ||||
| 	return crossFunction(d, matchingNodes, pathNode, performBoolOp( | ||||
| 	return crossFunction(d, matchingNodes, expressionNode, performBoolOp( | ||||
| 		func(b1 bool, b2 bool) bool { | ||||
| 			return b1 && b2 | ||||
| 		})) | ||||
| } | ||||
| 
 | ||||
| func notOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func notOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- notOperation") | ||||
| 	var results = list.New() | ||||
| 
 | ||||
|  | ||||
| @ -6,7 +6,7 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func collectOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func collectOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- collectOperation") | ||||
| 
 | ||||
| 	if matchMap.Len() == 0 { | ||||
|  | ||||
| @ -17,7 +17,7 @@ import ( | ||||
| ... | ||||
| */ | ||||
| 
 | ||||
| func collectObjectOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func collectObjectOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- collectObjectOperation") | ||||
| 
 | ||||
| 	if matchMap.Len() == 0 { | ||||
|  | ||||
| @ -13,21 +13,21 @@ type commentOpPreferences struct { | ||||
| 	FootComment bool | ||||
| } | ||||
| 
 | ||||
| func assignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func assignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	log.Debugf("AssignComments operator!") | ||||
| 
 | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 
 | ||||
| 	preferences := pathNode.Operation.Preferences.(*commentOpPreferences) | ||||
| 	preferences := expressionNode.Operation.Preferences.(*commentOpPreferences) | ||||
| 
 | ||||
| 	comment := "" | ||||
| 	if !pathNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	if !expressionNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @ -40,8 +40,8 @@ func assignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, path | ||||
| 	for el := lhs.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 
 | ||||
| 		if pathNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		if expressionNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| @ -66,8 +66,8 @@ func assignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, path | ||||
| 	return matchingNodes, nil | ||||
| } | ||||
| 
 | ||||
| func getCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	preferences := pathNode.Operation.Preferences.(*commentOpPreferences) | ||||
| func getCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	preferences := expressionNode.Operation.Preferences.(*commentOpPreferences) | ||||
| 	log.Debugf("GetComments operator!") | ||||
| 	var results = list.New() | ||||
| 
 | ||||
|  | ||||
| @ -6,7 +6,7 @@ import ( | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func createMapOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func createMapOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- createMapOperation") | ||||
| 
 | ||||
| 	//each matchingNodes entry should turn into a sequence of keys to create. | ||||
| @ -22,14 +22,14 @@ func createMapOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode | ||||
| 
 | ||||
| 		for matchingNodeEl := matchingNodes.Front(); matchingNodeEl != nil; matchingNodeEl = matchingNodeEl.Next() { | ||||
| 			matchingNode := matchingNodeEl.Value.(*CandidateNode) | ||||
| 			sequenceNode, err := sequenceFor(d, matchingNode, pathNode) | ||||
| 			sequenceNode, err := sequenceFor(d, matchingNode, expressionNode) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| 			sequences.PushBack(sequenceNode) | ||||
| 		} | ||||
| 	} else { | ||||
| 		sequenceNode, err := sequenceFor(d, nil, pathNode) | ||||
| 		sequenceNode, err := sequenceFor(d, nil, expressionNode) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @ -40,7 +40,7 @@ func createMapOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| func sequenceFor(d *dataTreeNavigator, matchingNode *CandidateNode, pathNode *PathTreeNode) (*CandidateNode, error) { | ||||
| func sequenceFor(d *dataTreeNavigator, matchingNode *CandidateNode, expressionNode *ExpressionNode) (*CandidateNode, error) { | ||||
| 	var path []interface{} | ||||
| 	var document uint = 0 | ||||
| 	var matches = list.New() | ||||
| @ -51,7 +51,7 @@ func sequenceFor(d *dataTreeNavigator, matchingNode *CandidateNode, pathNode *Pa | ||||
| 		matches = nodeToMap(matchingNode) | ||||
| 	} | ||||
| 
 | ||||
| 	mapPairs, err := crossFunction(d, matches, pathNode, | ||||
| 	mapPairs, err := crossFunction(d, matches, expressionNode, | ||||
| 		func(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { | ||||
| 			node := yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"} | ||||
| 			log.Debugf("LHS:", NodeToString(lhs)) | ||||
|  | ||||
| @ -7,9 +7,9 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func deleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func deleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	nodesToDelete, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	nodesToDelete, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @ -23,7 +23,7 @@ func deleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 			Value:         candidate.Path[len(candidate.Path)-1], | ||||
| 		} | ||||
| 
 | ||||
| 		deleteImmediateChildOpNode := &PathTreeNode{ | ||||
| 		deleteImmediateChildOpNode := &ExpressionNode{ | ||||
| 			Operation: deleteImmediateChildOp, | ||||
| 			Rhs:       createTraversalTree(candidate.Path[0 : len(candidate.Path)-1]), | ||||
| 		} | ||||
| @ -36,14 +36,14 @@ func deleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 	return matchingNodes, nil | ||||
| } | ||||
| 
 | ||||
| func deleteImmediateChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	parents, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| func deleteImmediateChildOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	parents, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 
 | ||||
| 	childPath := pathNode.Operation.Value | ||||
| 	childPath := expressionNode.Operation.Value | ||||
| 
 | ||||
| 	log.Debug("childPath to remove %v", childPath) | ||||
| 
 | ||||
|  | ||||
| @ -7,7 +7,7 @@ import ( | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func getDocumentIndexOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func getDocumentIndexOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	var results = list.New() | ||||
| 
 | ||||
| 	for el := matchingNodes.Front(); el != nil; el = el.Next() { | ||||
|  | ||||
| @ -13,13 +13,13 @@ type envOpPreferences struct { | ||||
| 	StringValue bool | ||||
| } | ||||
| 
 | ||||
| func envOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	envName := pathNode.Operation.CandidateNode.Node.Value | ||||
| func envOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	envName := expressionNode.Operation.CandidateNode.Node.Value | ||||
| 	log.Debug("EnvOperator, env name:", envName) | ||||
| 
 | ||||
| 	rawValue := os.Getenv(envName) | ||||
| 
 | ||||
| 	preferences := pathNode.Operation.Preferences.(*envOpPreferences) | ||||
| 	preferences := expressionNode.Operation.Preferences.(*envOpPreferences) | ||||
| 
 | ||||
| 	var node *yaml.Node | ||||
| 	if preferences.StringValue { | ||||
|  | ||||
| @ -4,9 +4,9 @@ import ( | ||||
| 	"container/list" | ||||
| ) | ||||
| 
 | ||||
| func equalsOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func equalsOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- equalsOperation") | ||||
| 	return crossFunction(d, matchingNodes, pathNode, isEquals) | ||||
| 	return crossFunction(d, matchingNodes, expressionNode, isEquals) | ||||
| } | ||||
| 
 | ||||
| func isEquals(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { | ||||
|  | ||||
| @ -7,7 +7,7 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func getFilenameOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func getFilenameOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("GetFilename") | ||||
| 
 | ||||
| 	var results = list.New() | ||||
| @ -22,7 +22,7 @@ func getFilenameOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 	return results, nil | ||||
| } | ||||
| 
 | ||||
| func getFileIndexOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func getFileIndexOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("GetFileIndex") | ||||
| 
 | ||||
| 	var results = list.New() | ||||
|  | ||||
| @ -7,12 +7,12 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func hasOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func hasOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	log.Debugf("-- hasOperation") | ||||
| 	var results = list.New() | ||||
| 
 | ||||
| 	rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 	wanted := rhs.Front().Value.(*CandidateNode).Node | ||||
| 	wantedKey := wanted.Value | ||||
| 
 | ||||
|  | ||||
| @ -7,7 +7,7 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func lengthOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func lengthOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- lengthOperation") | ||||
| 	var results = list.New() | ||||
| 
 | ||||
|  | ||||
| @ -10,14 +10,14 @@ import ( | ||||
| 
 | ||||
| type crossFunctionCalculation func(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) | ||||
| 
 | ||||
| func crossFunction(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode, calculation crossFunctionCalculation) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| func crossFunction(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode, calculation crossFunctionCalculation) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	log.Debugf("crossFunction LHS len: %v", lhs.Len()) | ||||
| 
 | ||||
| 	rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @ -47,9 +47,9 @@ type multiplyPreferences struct { | ||||
| 	AppendArrays bool | ||||
| } | ||||
| 
 | ||||
| func multiplyOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func multiplyOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- MultiplyOperator") | ||||
| 	return crossFunction(d, matchingNodes, pathNode, multiply(pathNode.Operation.Preferences.(*multiplyPreferences))) | ||||
| 	return crossFunction(d, matchingNodes, expressionNode, multiply(expressionNode.Operation.Preferences.(*multiplyPreferences))) | ||||
| } | ||||
| 
 | ||||
| func multiply(preferences *multiplyPreferences) func(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { | ||||
| @ -116,7 +116,7 @@ func applyAssignment(d *dataTreeNavigator, pathIndexToStartFrom int, lhs *Candid | ||||
| 	} | ||||
| 	rhsOp := &Operation{OperationType: valueOpType, CandidateNode: rhs} | ||||
| 
 | ||||
| 	assignmentOpNode := &PathTreeNode{Operation: assignmentOp, Lhs: createTraversalTree(lhsPath), Rhs: &PathTreeNode{Operation: rhsOp}} | ||||
| 	assignmentOpNode := &ExpressionNode{Operation: assignmentOp, Lhs: createTraversalTree(lhsPath), Rhs: &ExpressionNode{Operation: rhsOp}} | ||||
| 
 | ||||
| 	_, err := d.GetMatchingNodes(nodeToMap(lhs), assignmentOpNode) | ||||
| 
 | ||||
|  | ||||
| @ -16,7 +16,7 @@ func createPathNodeFor(pathElement interface{}) *yaml.Node { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func getPathOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func getPathOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("GetPath") | ||||
| 
 | ||||
| 	var results = list.New() | ||||
|  | ||||
| @ -2,10 +2,10 @@ package yqlib | ||||
| 
 | ||||
| import "container/list" | ||||
| 
 | ||||
| func pipeOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| func pipeOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return d.GetMatchingNodes(lhs, pathNode.Rhs) | ||||
| 	return d.GetMatchingNodes(lhs, expressionNode.Rhs) | ||||
| } | ||||
|  | ||||
| @ -11,10 +11,10 @@ type recursiveDescentPreferences struct { | ||||
| 	RecurseArray        bool | ||||
| } | ||||
| 
 | ||||
| func recursiveDescentOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func recursiveDescentOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	var results = list.New() | ||||
| 
 | ||||
| 	preferences := pathNode.Operation.Preferences.(*recursiveDescentPreferences) | ||||
| 	preferences := expressionNode.Operation.Preferences.(*recursiveDescentPreferences) | ||||
| 	err := recursiveDecent(d, results, matchMap, preferences) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
|  | ||||
| @ -4,7 +4,7 @@ import ( | ||||
| 	"container/list" | ||||
| ) | ||||
| 
 | ||||
| func selectOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func selectOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	log.Debugf("-- selectOperation") | ||||
| 	var results = list.New() | ||||
| @ -12,7 +12,7 @@ func selectOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *Pa | ||||
| 	for el := matchingNodes.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 
 | ||||
| 		rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
|  | ||||
| @ -2,6 +2,6 @@ package yqlib | ||||
| 
 | ||||
| import "container/list" | ||||
| 
 | ||||
| func selfOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func selfOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	return matchMap, nil | ||||
| } | ||||
|  | ||||
| @ -7,11 +7,11 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func sortKeysOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func sortKeysOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	for el := matchingNodes.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 		rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| @ -26,12 +26,12 @@ func parseStyle(customStyle string) (yaml.Style, error) { | ||||
| 	return 0, nil | ||||
| } | ||||
| 
 | ||||
| func assignStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func assignStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	log.Debugf("AssignStyleOperator: %v") | ||||
| 	var style yaml.Style | ||||
| 	if !pathNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	if !expressionNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @ -44,7 +44,7 @@ func assignStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @ -53,8 +53,8 @@ func assignStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 	for el := lhs.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 		log.Debugf("Setting style of : %v", candidate.GetKey()) | ||||
| 		if pathNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		if expressionNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| @ -73,7 +73,7 @@ func assignStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod | ||||
| 	return matchingNodes, nil | ||||
| } | ||||
| 
 | ||||
| func getStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func getStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("GetStyleOperator") | ||||
| 
 | ||||
| 	var results = list.New() | ||||
|  | ||||
| @ -6,13 +6,13 @@ import ( | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func assignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func assignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 
 | ||||
| 	log.Debugf("AssignTagOperator: %v") | ||||
| 	tag := "" | ||||
| 
 | ||||
| 	if !pathNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	if !expressionNode.Operation.UpdateAssign { | ||||
| 		rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @ -22,7 +22,7 @@ func assignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @ -31,8 +31,8 @@ func assignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode | ||||
| 	for el := lhs.Front(); el != nil; el = el.Next() { | ||||
| 		candidate := el.Value.(*CandidateNode) | ||||
| 		log.Debugf("Setting tag of : %v", candidate.GetKey()) | ||||
| 		if pathNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs) | ||||
| 		if expressionNode.Operation.UpdateAssign { | ||||
| 			rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| @ -47,7 +47,7 @@ func assignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode | ||||
| 	return matchingNodes, nil | ||||
| } | ||||
| 
 | ||||
| func getTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func getTagOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("GetTagOperator") | ||||
| 
 | ||||
| 	var results = list.New() | ||||
|  | ||||
| @ -18,12 +18,12 @@ func splat(d *dataTreeNavigator, matches *list.List, prefs *traversePreferences) | ||||
| 	return traverseNodesWithArrayIndices(matches, make([]*yaml.Node, 0), prefs) | ||||
| } | ||||
| 
 | ||||
| func traversePathOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func traversePathOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debugf("-- Traversing") | ||||
| 	var matchingNodeMap = list.New() | ||||
| 
 | ||||
| 	for el := matchMap.Front(); el != nil; el = el.Next() { | ||||
| 		newNodes, err := traverse(d, el.Value.(*CandidateNode), pathNode.Operation) | ||||
| 		newNodes, err := traverse(d, el.Value.(*CandidateNode), expressionNode.Operation) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @ -74,10 +74,10 @@ func traverse(d *dataTreeNavigator, matchingNode *CandidateNode, operation *Oper | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func traverseArrayOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func traverseArrayOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	// rhs is a collect expression that will yield indexes to retreive of the arrays | ||||
| 
 | ||||
| 	rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| @ -2,12 +2,12 @@ package yqlib | ||||
| 
 | ||||
| import "container/list" | ||||
| 
 | ||||
| func unionOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) | ||||
| func unionOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs) | ||||
| 	rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| @ -2,7 +2,7 @@ package yqlib | ||||
| 
 | ||||
| import "container/list" | ||||
| 
 | ||||
| func valueOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| 	log.Debug("value = %v", pathNode.Operation.CandidateNode.Node.Value) | ||||
| 	return nodeToMap(pathNode.Operation.CandidateNode), nil | ||||
| func valueOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	log.Debug("value = %v", expressionNode.Operation.CandidateNode.Node.Value) | ||||
| 	return nodeToMap(expressionNode.Operation.CandidateNode), nil | ||||
| } | ||||
|  | ||||
| @ -7,7 +7,7 @@ import ( | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| type operatorHandler func(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) | ||||
| type operatorHandler func(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) | ||||
| 
 | ||||
| func unwrapDoc(node *yaml.Node) *yaml.Node { | ||||
| 	if node.Kind == yaml.DocumentNode { | ||||
| @ -16,7 +16,7 @@ func unwrapDoc(node *yaml.Node) *yaml.Node { | ||||
| 	return node | ||||
| } | ||||
| 
 | ||||
| func emptyOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { | ||||
| func emptyOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) { | ||||
| 	return list.New(), nil | ||||
| } | ||||
| 
 | ||||
| @ -35,13 +35,13 @@ func nodeToMap(candidate *CandidateNode) *list.List { | ||||
| 	return elMap | ||||
| } | ||||
| 
 | ||||
| func createTraversalTree(path []interface{}) *PathTreeNode { | ||||
| func createTraversalTree(path []interface{}) *ExpressionNode { | ||||
| 	if len(path) == 0 { | ||||
| 		return &PathTreeNode{Operation: &Operation{OperationType: selfReferenceOpType}} | ||||
| 		return &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}} | ||||
| 	} else if len(path) == 1 { | ||||
| 		return &PathTreeNode{Operation: &Operation{OperationType: traversePathOpType, Value: path[0], StringValue: fmt.Sprintf("%v", path[0])}} | ||||
| 		return &ExpressionNode{Operation: &Operation{OperationType: traversePathOpType, Value: path[0], StringValue: fmt.Sprintf("%v", path[0])}} | ||||
| 	} | ||||
| 	return &PathTreeNode{ | ||||
| 	return &ExpressionNode{ | ||||
| 		Operation: &Operation{OperationType: shortPipeOpType}, | ||||
| 		Lhs:       createTraversalTree(path[0:1]), | ||||
| 		Rhs:       createTraversalTree(path[1:])} | ||||
|  | ||||
| @ -30,7 +30,7 @@ func testScenario(t *testing.T, s *expressionScenario) { | ||||
| 	var results *list.List | ||||
| 	var err error | ||||
| 
 | ||||
| 	node, err := NewPathTreeCreator().ParsePath(s.expression) | ||||
| 	node, err := NewExpressionParser().ParseExpression(s.expression) | ||||
| 	if err != nil { | ||||
| 		t.Error(fmt.Errorf("Error parsing expression %v of %v: %v", s.expression, s.description, err)) | ||||
| 		return | ||||
| @ -110,7 +110,7 @@ func formatYaml(yaml string, filename string) string { | ||||
| 	var output bytes.Buffer | ||||
| 	printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true) | ||||
| 
 | ||||
| 	node, err := NewPathTreeCreator().ParsePath(".. style= \"\"") | ||||
| 	node, err := NewExpressionParser().ParseExpression(".. style= \"\"") | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| @ -219,7 +219,7 @@ func documentOutput(t *testing.T, w *bufio.Writer, s expressionScenario, formatt | ||||
| 	var err error | ||||
| 	printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true) | ||||
| 
 | ||||
| 	node, err := NewPathTreeCreator().ParsePath(s.expression) | ||||
| 	node, err := NewExpressionParser().ParseExpression(s.expression) | ||||
| 	if err != nil { | ||||
| 		t.Error(fmt.Errorf("Error parsing expression %v of %v: %v", s.expression, s.description, err)) | ||||
| 		return | ||||
|  | ||||
| @ -77,7 +77,7 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error { | ||||
| 	var err error | ||||
| 	if p.outputToJSON { | ||||
| 		explodeOp := Operation{OperationType: explodeOpType} | ||||
| 		explodeNode := PathTreeNode{Operation: &explodeOp} | ||||
| 		explodeNode := ExpressionNode{Operation: &explodeOp} | ||||
| 		matchingNodes, err = p.treeNavigator.GetMatchingNodes(matchingNodes, &explodeNode) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
|  | ||||
| @ -12,23 +12,23 @@ import ( | ||||
| // Uses less memory than loading all documents and running the expression once, but this cannot process | ||||
| // cross document expressions. | ||||
| type StreamEvaluator interface { | ||||
| 	Evaluate(filename string, reader io.Reader, node *PathTreeNode, printer Printer) error | ||||
| 	Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) error | ||||
| 	EvaluateFiles(expression string, filenames []string, printer Printer) error | ||||
| 	EvaluateNew(expression string, printer Printer) error | ||||
| } | ||||
| 
 | ||||
| type streamEvaluator struct { | ||||
| 	treeNavigator DataTreeNavigator | ||||
| 	treeCreator   PathTreeCreator | ||||
| 	treeCreator   ExpressionParser | ||||
| 	fileIndex     int | ||||
| } | ||||
| 
 | ||||
| func NewStreamEvaluator() StreamEvaluator { | ||||
| 	return &streamEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewPathTreeCreator()} | ||||
| 	return &streamEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewExpressionParser()} | ||||
| } | ||||
| 
 | ||||
| func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error { | ||||
| 	node, err := s.treeCreator.ParsePath(expression) | ||||
| 	node, err := s.treeCreator.ParseExpression(expression) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @ -50,7 +50,7 @@ func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error | ||||
| 
 | ||||
| func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error { | ||||
| 
 | ||||
| 	node, err := s.treeCreator.ParsePath(expression) | ||||
| 	node, err := s.treeCreator.ParseExpression(expression) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @ -73,7 +73,7 @@ func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, p | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *PathTreeNode, printer Printer) error { | ||||
| func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) error { | ||||
| 
 | ||||
| 	var currentIndex uint | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user