mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	Moved eval function to eval interface
This commit is contained in:
		
							parent
							
								
									feda9f044d
								
							
						
					
					
						commit
						43165fa340
					
				| @ -1,14 +1,20 @@ | ||||
| package yqlib | ||||
| 
 | ||||
| import "container/list" | ||||
| import ( | ||||
| 	"container/list" | ||||
| 
 | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| // A yaml expression evaluator that runs the expression once against all files/nodes in memory. | ||||
| type Evaluator interface { | ||||
| 	EvaluateFiles(expression string, filenames []string, printer Printer) error | ||||
| 
 | ||||
| 	// Runs the expression once against the list of candidate nodes, returns the | ||||
| 	// resulting nodes. | ||||
| 	EvaluateNodes(expression string, inputCandidateNodes *list.List) (*list.List, error) | ||||
| 	// EvaluateNodes takes an expression and one or more yaml nodes, returning a list of matching candidate nodes | ||||
| 	EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) | ||||
| 
 | ||||
| 	// EvaluateCandidateNodes takes an expression and list of candidate nodes, returning a list of matching candidate nodes | ||||
| 	EvaluateCandidateNodes(expression string, inputCandidateNodes *list.List) (*list.List, error) | ||||
| } | ||||
| 
 | ||||
| type allAtOnceEvaluator struct { | ||||
| @ -20,7 +26,15 @@ func NewAllAtOnceEvaluator() Evaluator { | ||||
| 	return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewPathTreeCreator()} | ||||
| } | ||||
| 
 | ||||
| func (e *allAtOnceEvaluator) EvaluateNodes(expression string, inputCandidates *list.List) (*list.List, error) { | ||||
| func (e *allAtOnceEvaluator) EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) { | ||||
| 	inputCandidates := list.New() | ||||
| 	for _, node := range nodes { | ||||
| 		inputCandidates.PushBack(&CandidateNode{Node: node}) | ||||
| 	} | ||||
| 	return e.EvaluateCandidateNodes(expression, inputCandidates) | ||||
| } | ||||
| 
 | ||||
| func (e *allAtOnceEvaluator) EvaluateCandidateNodes(expression string, inputCandidates *list.List) (*list.List, error) { | ||||
| 	node, err := treeCreator.ParsePath(expression) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @ -44,7 +58,7 @@ func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string | ||||
| 		allDocuments.PushBackList(fileDocuments) | ||||
| 		fileIndex = fileIndex + 1 | ||||
| 	} | ||||
| 	matches, err := e.EvaluateNodes(expression, allDocuments) | ||||
| 	matches, err := e.EvaluateCandidateNodes(expression, allDocuments) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| @ -30,10 +30,11 @@ var evaluateNodesScenario = []expressionScenario{ | ||||
| 	}, | ||||
| } | ||||
| 
 | ||||
| func TestEvaluateNodesScenarios(t *testing.T) { | ||||
| func TestAllAtOnceEvaluateNodes(t *testing.T) { | ||||
| 	var evaluator = NewAllAtOnceEvaluator() | ||||
| 	for _, tt := range evaluateNodesScenario { | ||||
| 		node := test.ParseData(tt.document) | ||||
| 		list, _ := EvaluateNodes(tt.expression, &node) | ||||
| 		list, _ := evaluator.EvaluateNodes(tt.expression, &node) | ||||
| 		test.AssertResultComplex(t, tt.expected, resultsToString(list)) | ||||
| 	} | ||||
| } | ||||
| @ -1,3 +1,5 @@ | ||||
| // Use the top level Evaluator or StreamEvaluator to evaluate expressions and return matches. | ||||
| // | ||||
| package yqlib | ||||
| 
 | ||||
| import ( | ||||
| @ -166,24 +168,6 @@ func NodeToString(node *CandidateNode) string { | ||||
| 	return fmt.Sprintf(`D%v, P%v, (%v)::%v`, node.Document, node.Path, tag, buf.String()) | ||||
| } | ||||
| 
 | ||||
| // EvaluateNodes takes an expression and one or more yaml nodes, returning a list of matching candidate nodes | ||||
| func EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) { | ||||
| 	inputCandidates := list.New() | ||||
| 	for _, node := range nodes { | ||||
| 		inputCandidates.PushBack(&CandidateNode{Node: node}) | ||||
| 	} | ||||
| 	return EvaluateCandidateNodes(expression, inputCandidates) | ||||
| } | ||||
| 
 | ||||
| // EvaluateCandidateNodes takes an expression and list of candidate nodes, returning a list of matching candidate nodes | ||||
| func EvaluateCandidateNodes(expression string, inputCandidates *list.List) (*list.List, error) { | ||||
| 	node, err := treeCreator.ParsePath(expression) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return treeNavigator.GetMatchingNodes(inputCandidates, node) | ||||
| } | ||||
| 
 | ||||
| func KindString(kind yaml.Kind) string { | ||||
| 	switch kind { | ||||
| 	case yaml.ScalarNode: | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user