mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	can assign values
This commit is contained in:
		
							parent
							
								
									6829d8cb78
								
							
						
					
					
						commit
						fccd03036f
					
				| @ -72,6 +72,8 @@ func (d *dataTreeNavigator) getMatchingNodes(matchingNodes *orderedmap.OrderedMa | ||||
| 		return matchingNodes, nil | ||||
| 	} else if pathNode.PathElement.PathElementType == PathKey { | ||||
| 		return d.traverse(matchingNodes, pathNode.PathElement) | ||||
| 	} else if pathNode.PathElement.PathElementType == Value { | ||||
| 		return nodeToMap(BuildCandidateNodeFrom(pathNode.PathElement)), nil | ||||
| 	} else { | ||||
| 		handler := pathNode.PathElement.OperationType.Handler | ||||
| 		if handler != nil { | ||||
|  | ||||
| @ -625,12 +625,12 @@ func TestDataTreeNavigatorArraySimple(t *testing.T) { | ||||
| 	test.AssertResult(t, expected, resultsToString(results)) | ||||
| } | ||||
| 
 | ||||
| func TestDataTreeNavigatorSimpleAssign(t *testing.T) { | ||||
| func TestDataTreeNavigatorSimpleAssignCmd(t *testing.T) { | ||||
| 
 | ||||
| 	nodes := readDoc(t, `a:  | ||||
|   b: apple`) | ||||
| 
 | ||||
| 	path, errPath := treeCreator.ParsePath("a.b := frog") | ||||
| 	path, errPath := treeCreator.ParsePath(`.a.b |= "frog"`) | ||||
| 	if errPath != nil { | ||||
| 		t.Error(errPath) | ||||
| 	} | ||||
| @ -650,6 +650,80 @@ func TestDataTreeNavigatorSimpleAssign(t *testing.T) { | ||||
| 	test.AssertResult(t, expected, resultsToString(results)) | ||||
| } | ||||
| 
 | ||||
| func TestDataTreeNavigatorSimpleAssignNumberCmd(t *testing.T) { | ||||
| 
 | ||||
| 	nodes := readDoc(t, `a:  | ||||
|   b: apple`) | ||||
| 
 | ||||
| 	path, errPath := treeCreator.ParsePath(`.a.b |= 5`) | ||||
| 	if errPath != nil { | ||||
| 		t.Error(errPath) | ||||
| 	} | ||||
| 	results, errNav := treeNavigator.GetMatchingNodes(nodes, path) | ||||
| 
 | ||||
| 	if errNav != nil { | ||||
| 		t.Error(errNav) | ||||
| 	} | ||||
| 
 | ||||
| 	expected := ` | ||||
| -- Node -- | ||||
|   Document 0, path: [a b] | ||||
|   Tag: !!int, Kind: ScalarNode, Anchor:  | ||||
|   5 | ||||
| ` | ||||
| 
 | ||||
| 	test.AssertResult(t, expected, resultsToString(results)) | ||||
| } | ||||
| 
 | ||||
| func TestDataTreeNavigatorSimpleAssignFloatCmd(t *testing.T) { | ||||
| 
 | ||||
| 	nodes := readDoc(t, `a:  | ||||
|   b: apple`) | ||||
| 
 | ||||
| 	path, errPath := treeCreator.ParsePath(`.a.b |= 3.142`) | ||||
| 	if errPath != nil { | ||||
| 		t.Error(errPath) | ||||
| 	} | ||||
| 	results, errNav := treeNavigator.GetMatchingNodes(nodes, path) | ||||
| 
 | ||||
| 	if errNav != nil { | ||||
| 		t.Error(errNav) | ||||
| 	} | ||||
| 
 | ||||
| 	expected := ` | ||||
| -- Node -- | ||||
|   Document 0, path: [a b] | ||||
|   Tag: !!float, Kind: ScalarNode, Anchor:  | ||||
|   3.142 | ||||
| ` | ||||
| 
 | ||||
| 	test.AssertResult(t, expected, resultsToString(results)) | ||||
| } | ||||
| 
 | ||||
| func TestDataTreeNavigatorSimpleAssignBooleanCmd(t *testing.T) { | ||||
| 
 | ||||
| 	nodes := readDoc(t, `a:  | ||||
|   b: apple`) | ||||
| 	path, errPath := treeCreator.ParsePath(`.a.b |= true`) | ||||
| 	if errPath != nil { | ||||
| 		t.Error(errPath) | ||||
| 	} | ||||
| 	results, errNav := treeNavigator.GetMatchingNodes(nodes, path) | ||||
| 
 | ||||
| 	if errNav != nil { | ||||
| 		t.Error(errNav) | ||||
| 	} | ||||
| 
 | ||||
| 	expected := ` | ||||
| -- Node -- | ||||
|   Document 0, path: [a b] | ||||
|   Tag: !!bool, Kind: ScalarNode, Anchor:  | ||||
|   true | ||||
| ` | ||||
| 
 | ||||
| 	test.AssertResult(t, expected, resultsToString(results)) | ||||
| } | ||||
| 
 | ||||
| func TestDataTreeNavigatorSimpleAssignSelf(t *testing.T) { | ||||
| 
 | ||||
| 	nodes := readDoc(t, `a:  | ||||
|  | ||||
							
								
								
									
										20
									
								
								pkg/yqlib/treeops/value_node_builder.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								pkg/yqlib/treeops/value_node_builder.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,20 @@ | ||||
| package treeops | ||||
| 
 | ||||
| import "gopkg.in/yaml.v3" | ||||
| 
 | ||||
| func BuildCandidateNodeFrom(p *PathElement) *CandidateNode { | ||||
| 	var node yaml.Node = yaml.Node{Kind: yaml.ScalarNode} | ||||
| 	node.Value = p.StringValue | ||||
| 
 | ||||
| 	switch p.Value.(type) { | ||||
| 	case float32, float64: | ||||
| 		node.Tag = "!!float" | ||||
| 	case int, int64, int32: | ||||
| 		node.Tag = "!!int" | ||||
| 	case bool: | ||||
| 		node.Tag = "!!bool" | ||||
| 	case string: | ||||
| 		node.Tag = "!!str" | ||||
| 	} | ||||
| 	return &CandidateNode{Node: &node} | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user