mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	Multiply wip
This commit is contained in:
		
							parent
							
								
									391ab8d70c
								
							
						
					
					
						commit
						49b810cedd
					
				| @ -40,6 +40,8 @@ var Union = &OperationType{Type: "UNION", NumArgs: 2, Precedence: 10, Handler: U | |||||||
| var Intersection = &OperationType{Type: "INTERSECTION", NumArgs: 2, Precedence: 20, Handler: IntersectionOperator} | var Intersection = &OperationType{Type: "INTERSECTION", NumArgs: 2, Precedence: 20, Handler: IntersectionOperator} | ||||||
| 
 | 
 | ||||||
| var Assign = &OperationType{Type: "ASSIGN", NumArgs: 2, Precedence: 40, Handler: AssignOperator} | var Assign = &OperationType{Type: "ASSIGN", NumArgs: 2, Precedence: 40, Handler: AssignOperator} | ||||||
|  | var Multiply = &OperationType{Type: "MULTIPLY", NumArgs: 2, Precedence: 40, Handler: MultiplyOperator} | ||||||
|  | 
 | ||||||
| var Equals = &OperationType{Type: "EQUALS", NumArgs: 2, Precedence: 40, Handler: EqualsOperator} | var Equals = &OperationType{Type: "EQUALS", NumArgs: 2, Precedence: 40, Handler: EqualsOperator} | ||||||
| var Pipe = &OperationType{Type: "PIPE", NumArgs: 2, Precedence: 45, Handler: PipeOperator} | var Pipe = &OperationType{Type: "PIPE", NumArgs: 2, Precedence: 45, Handler: PipeOperator} | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										20
									
								
								pkg/yqlib/treeops/operator_merge.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								pkg/yqlib/treeops/operator_merge.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,20 @@ | |||||||
|  | package treeops | ||||||
|  | 
 | ||||||
|  | import "github.com/elliotchance/orderedmap" | ||||||
|  | 
 | ||||||
|  | func MultiplyOperator(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) { | ||||||
|  | 	lhs, err := d.getMatchingNodes(matchingNodes, pathNode.Lhs) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	for el := lhs.Front(); el != nil; el = el.Next() { | ||||||
|  | 		candidate := el.Value.(*CandidateNode) | ||||||
|  | 
 | ||||||
|  | 		// TODO handle scalar mulitplication | ||||||
|  | 		switch candidate.Node.Kind { | ||||||
|  | 			case  | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 	} | ||||||
|  | 	return matchingNodes, nil | ||||||
|  | } | ||||||
							
								
								
									
										27
									
								
								pkg/yqlib/treeops/operator_merge_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								pkg/yqlib/treeops/operator_merge_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | |||||||
|  | package treeops | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"testing" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var mergeOperatorScenarios = []expressionScenario{ | ||||||
|  | 	{ | ||||||
|  | 		document:   `{a: frog, b: cat}`, | ||||||
|  | 		expression: `.a * .b`, | ||||||
|  | 		expected: []string{ | ||||||
|  | 			"D0, P[], (!!map)::{a: cat, b: cat}\n", | ||||||
|  | 		}, | ||||||
|  | 	}, { | ||||||
|  | 		document:   `{a: {things: great}, b: {also: me}}`, | ||||||
|  | 		expression: `.a * .b`, | ||||||
|  | 		expected: []string{ | ||||||
|  | 			"D0, P[], (!!map)::{a: {also: me, things: great}, b: {also: me}}\n", | ||||||
|  | 		}, | ||||||
|  | 	}, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestMergeOperatorScenarios(t *testing.T) { | ||||||
|  | 	for _, tt := range mergeOperatorScenarios { | ||||||
|  | 		testScenario(t, &tt) | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @ -153,6 +153,7 @@ func initLexer() (*lex.Lexer, error) { | |||||||
| 
 | 
 | ||||||
| 	lexer.Add([]byte(`\[`), literalToken(OpenCollect, "[", false)) | 	lexer.Add([]byte(`\[`), literalToken(OpenCollect, "[", false)) | ||||||
| 	lexer.Add([]byte(`\]`), literalToken(CloseCollect, "]", true)) | 	lexer.Add([]byte(`\]`), literalToken(CloseCollect, "]", true)) | ||||||
|  | 	lexer.Add([]byte(`\*`), opToken(Multiply)) | ||||||
| 
 | 
 | ||||||
| 	// lexer.Add([]byte(`[^ \,\|\.\[\(\)=]+`), stringValue(false)) | 	// lexer.Add([]byte(`[^ \,\|\.\[\(\)=]+`), stringValue(false)) | ||||||
| 	err := lexer.Compile() | 	err := lexer.Compile() | ||||||
|  | |||||||
							
								
								
									
										1
									
								
								pkg/yqlib/treeops/sample.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								pkg/yqlib/treeops/sample.yaml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | {a: {b: apple, c: cactus}} | ||||||
							
								
								
									
										6
									
								
								pkg/yqlib/treeops/temp.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								pkg/yqlib/treeops/temp.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | |||||||
|  | { | ||||||
|  |   "a": { | ||||||
|  |     "b": "apple", | ||||||
|  |     "c": "cactus" | ||||||
|  |   } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user