mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	Fixed length of null to be zero
This commit is contained in:
		
							parent
							
								
									071ec3c08c
								
							
						
					
					
						commit
						f46fe384bd
					
				| @ -16,6 +16,20 @@ will output | ||||
| 3 | ||||
| ``` | ||||
| 
 | ||||
| ## null length | ||||
| Given a sample.yml file of: | ||||
| ```yaml | ||||
| a: null | ||||
| ``` | ||||
| then | ||||
| ```bash | ||||
| yq eval '.a | length' sample.yml | ||||
| ``` | ||||
| will output | ||||
| ```yaml | ||||
| 0 | ||||
| ``` | ||||
| 
 | ||||
| ## Map length | ||||
| returns number of entries | ||||
| 
 | ||||
|  | ||||
| @ -17,7 +17,11 @@ func lengthOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *E | ||||
| 		var length int | ||||
| 		switch targetNode.Kind { | ||||
| 		case yaml.ScalarNode: | ||||
| 			length = len(targetNode.Value) | ||||
| 			if targetNode.Tag == "!!null" { | ||||
| 				length = 0 | ||||
| 			} else { | ||||
| 				length = len(targetNode.Value) | ||||
| 			} | ||||
| 		case yaml.MappingNode: | ||||
| 			length = len(targetNode.Content) / 2 | ||||
| 		case yaml.SequenceNode: | ||||
|  | ||||
| @ -14,6 +14,30 @@ var lengthOperatorScenarios = []expressionScenario{ | ||||
| 			"D0, P[a], (!!int)::3\n", | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		description: "null length", | ||||
| 		document:    `{a: null}`, | ||||
| 		expression:  `.a | length`, | ||||
| 		expected: []string{ | ||||
| 			"D0, P[a], (!!int)::0\n", | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		skipDoc:    true, | ||||
| 		document:   `{a: ~}`, | ||||
| 		expression: `.a | length`, | ||||
| 		expected: []string{ | ||||
| 			"D0, P[a], (!!int)::0\n", | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		skipDoc:    true, | ||||
| 		document:   `{a: key no exist}`, | ||||
| 		expression: `.b | length`, | ||||
| 		expected: []string{ | ||||
| 			"D0, P[b], (!!int)::0\n", | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		description:    "Map length", | ||||
| 		subdescription: "returns number of entries", | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user