From 820a3320be10bc925877afc78cdc0bec97e4f8e2 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 26 Jan 2021 10:21:16 +1100 Subject: [PATCH] Fixed length of null to be zero --- pkg/yqlib/doc/Length.md | 14 ++++++++++++++ pkg/yqlib/operator_length.go | 6 +++++- pkg/yqlib/operator_length_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pkg/yqlib/doc/Length.md b/pkg/yqlib/doc/Length.md index dcd76562..0f7e73d7 100644 --- a/pkg/yqlib/doc/Length.md +++ b/pkg/yqlib/doc/Length.md @@ -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 diff --git a/pkg/yqlib/operator_length.go b/pkg/yqlib/operator_length.go index b9d6dded..70888d2e 100644 --- a/pkg/yqlib/operator_length.go +++ b/pkg/yqlib/operator_length.go @@ -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: diff --git a/pkg/yqlib/operator_length_test.go b/pkg/yqlib/operator_length_test.go index 353f2ef8..e9ecc2d0 100644 --- a/pkg/yqlib/operator_length_test.go +++ b/pkg/yqlib/operator_length_test.go @@ -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",