mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-13 22:38:04 +00:00
Fixed length of null to be zero
This commit is contained in:
parent
61f569aebb
commit
820a3320be
@ -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…
Reference in New Issue
Block a user