From c469e80cad74b417e0d5aa946a98d2e0315b5cc6 Mon Sep 17 00:00:00 2001 From: jregan Date: Sun, 9 Aug 2020 12:36:15 -0700 Subject: [PATCH] Add RNode.IsNilOrEmpty and test. --- kyaml/yaml/types.go | 9 +++++++++ kyaml/yaml/types_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index 6750c5be3..cdaa6ecdb 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -383,6 +383,15 @@ func (rn *RNode) IsTaggedNull() bool { return !rn.IsNil() && IsYNodeTaggedNull(rn.YNode()) } +// IsNilOrEmpty is true if the node is nil, +// has no YNode, or has YNode that appears empty. +func (rn *RNode) IsNilOrEmpty() bool { + return rn.IsNil() || + IsYNodeTaggedNull(rn.YNode()) || + IsYNodeEmptyMap(rn.YNode()) || + IsYNodeEmptySeq(rn.YNode()) +} + // GetMeta returns the ResourceMeta for an RNode func (rn *RNode) GetMeta() (ResourceMeta, error) { if IsMissingOrNull(rn) { diff --git a/kyaml/yaml/types_test.go b/kyaml/yaml/types_test.go index 0e4447ddf..2b3da83a3 100644 --- a/kyaml/yaml/types_test.go +++ b/kyaml/yaml/types_test.go @@ -323,3 +323,35 @@ func TestIsTaggedNull(t *testing.T) { t.Fatalf("empty list should not be tagged null") } } + +func TestRNodeIsNilOrEmpty(t *testing.T) { + var rn *RNode + + if !rn.IsNilOrEmpty() { + t.Fatalf("uninitialized RNode should be empty") + } + + if !NewRNode(nil).IsNilOrEmpty() { + t.Fatalf("missing value YNode should be empty") + } + + if !MakeNullNode().IsNilOrEmpty() { + t.Fatalf("value tagged null should be empty") + } + + if !NewMapRNode(nil).IsNilOrEmpty() { + t.Fatalf("empty map should be empty") + } + + if NewMapRNode(&map[string]string{"foo": "bar"}).IsNilOrEmpty() { + t.Fatalf("non-empty map should not be empty") + } + + if !NewListRNode().IsNilOrEmpty() { + t.Fatalf("empty list should be empty") + } + + if NewListRNode("foo").IsNilOrEmpty() { + t.Fatalf("non-empty list should not be empty") + } +}