From 31c59bd7f245a712157dc9285189844d3b9b81b1 Mon Sep 17 00:00:00 2001 From: jregan Date: Sun, 9 Aug 2020 10:39:04 -0700 Subject: [PATCH] Drop all calls to IsEmpty. --- api/filters/namespace/namespace.go | 4 +-- kyaml/fn/runtime/runtimeutil/functiontypes.go | 2 +- kyaml/openapi/openapi.go | 2 +- kyaml/setters2/list.go | 8 +++--- kyaml/setters2/walk.go | 2 +- kyaml/yaml/fns.go | 4 +-- kyaml/yaml/merge2/merge2.go | 4 +-- kyaml/yaml/merge3/visitor.go | 12 ++++----- kyaml/yaml/schema/schema.go | 2 +- kyaml/yaml/types.go | 11 ++++---- kyaml/yaml/types_test.go | 25 +++---------------- kyaml/yaml/walk/associative_sequence.go | 8 +++--- kyaml/yaml/walk/map.go | 4 +-- kyaml/yaml/walk/walk.go | 6 ++--- 14 files changed, 39 insertions(+), 55 deletions(-) diff --git a/api/filters/namespace/namespace.go b/api/filters/namespace/namespace.go index 259977e72..7941af5a6 100644 --- a/api/filters/namespace/namespace.go +++ b/api/filters/namespace/namespace.go @@ -112,7 +112,7 @@ func (ns Filter) roleBindingHack(obj *yaml.RNode, meta yaml.ResourceMeta) error // Lookup the namespace field on all elements. // We should change the fieldspec so this isn't necessary. obj, err := obj.Pipe(yaml.Lookup(subjectsField)) - if err != nil || yaml.IsEmpty(obj) { + if err != nil || yaml.IsMissingOrNull(obj) { return err } @@ -125,7 +125,7 @@ func (ns Filter) roleBindingHack(obj *yaml.RNode, meta yaml.ResourceMeta) error name, err := o.Pipe( yaml.Lookup("name"), yaml.Match("default"), ) - if err != nil || yaml.IsEmpty(name) { + if err != nil || yaml.IsMissingOrNull(name) { return err } diff --git a/kyaml/fn/runtime/runtimeutil/functiontypes.go b/kyaml/fn/runtime/runtimeutil/functiontypes.go index 6e476e871..29bb9be73 100644 --- a/kyaml/fn/runtime/runtimeutil/functiontypes.go +++ b/kyaml/fn/runtime/runtimeutil/functiontypes.go @@ -138,7 +138,7 @@ func getFunctionSpecFromAnnotation(n *yaml.RNode, meta yaml.ResourceMeta) *Funct } } n, err := n.Pipe(yaml.Lookup("metadata", "configFn")) - if err != nil || yaml.IsEmpty(n) { + if err != nil || yaml.IsMissingOrNull(n) { return nil } s, err := n.String() diff --git a/kyaml/openapi/openapi.go b/kyaml/openapi/openapi.go index 08d161d04..3f1272b77 100644 --- a/kyaml/openapi/openapi.go +++ b/kyaml/openapi/openapi.go @@ -36,7 +36,7 @@ type ResourceSchema struct { } // IsEmpty returns true if the ResourceSchema is empty -func (rs *ResourceSchema) IsEmpty() bool { +func (rs *ResourceSchema) IsMissingOrNull() bool { if rs == nil || rs.Schema == nil { return true } diff --git a/kyaml/setters2/list.go b/kyaml/setters2/list.go index 33e2906a2..5116500a7 100644 --- a/kyaml/setters2/list.go +++ b/kyaml/setters2/list.go @@ -53,7 +53,7 @@ func (l *List) listSetters(object *yaml.RNode, resourcePath string) error { if err != nil { return err } - if yaml.IsEmpty(def) { + if yaml.IsMissingOrNull(def) { return nil } @@ -73,7 +73,7 @@ func (l *List) listSetters(object *yaml.RNode, resourcePath string) error { if err != nil { return err } - if yaml.IsEmpty(setterNode) { + if yaml.IsMissingOrNull(setterNode) { // has the setter prefix, but missing the setter extension return errors.Errorf("missing x-k8s-cli.setter for %s", key) } @@ -126,7 +126,7 @@ func (l *List) listSubst(object *yaml.RNode) error { if err != nil { return err } - if yaml.IsEmpty(def) { + if yaml.IsMissingOrNull(def) { return nil } @@ -146,7 +146,7 @@ func (l *List) listSubst(object *yaml.RNode) error { if err != nil { return err } - if yaml.IsEmpty(substNode) { + if yaml.IsMissingOrNull(substNode) { // has the substitution prefix, but missing the setter extension return errors.Errorf("missing x-k8s-cli.substitution for %s", key) } diff --git a/kyaml/setters2/walk.go b/kyaml/setters2/walk.go index 619cad0c6..69eb1f447 100644 --- a/kyaml/setters2/walk.go +++ b/kyaml/setters2/walk.go @@ -110,7 +110,7 @@ func getSchema(r *yaml.RNode, s *openapi.ResourceSchema, field string) *openapi. return s } - if yaml.IsEmpty(r) { + if yaml.IsMissingOrNull(r) { return nil } diff --git a/kyaml/yaml/fns.go b/kyaml/yaml/fns.go index 2edc5179e..4568d9530 100644 --- a/kyaml/yaml/fns.go +++ b/kyaml/yaml/fns.go @@ -70,7 +70,7 @@ func (e ElementSetter) Filter(rn *RNode) (*RNode, error) { newNode := NewRNode(elem) // empty elements are not valid -- they at least need an associative key - if IsEmpty(newNode) || IsEmptyMap(newNode) { + if IsMissingOrNull(newNode) || IsEmptyMap(newNode) { continue } @@ -549,7 +549,7 @@ func IsFoundOrError(rn *RNode, err error) bool { func ErrorIfAnyInvalidAndNonNull(kind yaml.Kind, rn ...*RNode) error { for i := range rn { - if IsEmpty(rn[i]) { + if IsMissingOrNull(rn[i]) { continue } if err := ErrorIfInvalid(rn[i], kind); err != nil { diff --git a/kyaml/yaml/merge2/merge2.go b/kyaml/yaml/merge2/merge2.go index a2840cd00..2d0dd32de 100644 --- a/kyaml/yaml/merge2/merge2.go +++ b/kyaml/yaml/merge2/merge2.go @@ -52,7 +52,7 @@ func (m Merger) VisitMap(nodes walk.Sources, s *openapi.ResourceSchema) (*yaml.R if err := m.SetStyle(nodes); err != nil { return nil, err } - if yaml.IsEmpty(nodes.Dest()) { + if yaml.IsMissingOrNull(nodes.Dest()) { // Add return nodes.Origin(), nil } @@ -108,7 +108,7 @@ func (m Merger) VisitList(nodes walk.Sources, s *openapi.ResourceSchema, kind wa } // Add - if yaml.IsEmpty(nodes.Dest()) { + if yaml.IsMissingOrNull(nodes.Dest()) { return nodes.Origin(), nil } // Clear diff --git a/kyaml/yaml/merge3/visitor.go b/kyaml/yaml/merge3/visitor.go index 3ae382b8b..43d62e70e 100644 --- a/kyaml/yaml/merge3/visitor.go +++ b/kyaml/yaml/merge3/visitor.go @@ -39,11 +39,11 @@ func (m Visitor) VisitMap(nodes walk.Sources, s *openapi.ResourceSchema) (*yaml. } func (m Visitor) visitAList(nodes walk.Sources, _ *openapi.ResourceSchema) (*yaml.RNode, error) { - if yaml.IsEmpty(nodes.Updated()) && !yaml.IsEmpty(nodes.Origin()) { + if yaml.IsMissingOrNull(nodes.Updated()) && !yaml.IsMissingOrNull(nodes.Origin()) { // implicitly cleared from update -- element was deleted return walk.ClearNode, nil } - if yaml.IsEmpty(nodes.Dest()) { + if yaml.IsMissingOrNull(nodes.Dest()) { // not cleared, but missing from the dest // initialize a new value that can be recursively merged return yaml.NewRNode(&yaml.Node{Kind: yaml.SequenceNode}), nil @@ -58,11 +58,11 @@ func (m Visitor) VisitScalar(nodes walk.Sources, s *openapi.ResourceSchema) (*ya // explicitly cleared from either dest or update return nil, nil } - if yaml.IsEmpty(nodes.Updated()) != yaml.IsEmpty(nodes.Origin()) { + if yaml.IsMissingOrNull(nodes.Updated()) != yaml.IsMissingOrNull(nodes.Origin()) { // value added or removed in update return nodes.Updated(), nil } - if yaml.IsEmpty(nodes.Updated()) && yaml.IsEmpty(nodes.Origin()) { + if yaml.IsMissingOrNull(nodes.Updated()) && yaml.IsMissingOrNull(nodes.Origin()) { // value added or removed in update return nodes.Dest(), nil } @@ -96,11 +96,11 @@ func (m Visitor) visitNAList(nodes walk.Sources) (*yaml.RNode, error) { return walk.ClearNode, nil } - if yaml.IsEmpty(nodes.Updated()) != yaml.IsEmpty(nodes.Origin()) { + if yaml.IsMissingOrNull(nodes.Updated()) != yaml.IsMissingOrNull(nodes.Origin()) { // value added or removed in update return nodes.Updated(), nil } - if yaml.IsEmpty(nodes.Updated()) && yaml.IsEmpty(nodes.Origin()) { + if yaml.IsMissingOrNull(nodes.Updated()) && yaml.IsMissingOrNull(nodes.Origin()) { // value not present in source or dest return nodes.Dest(), nil } diff --git a/kyaml/yaml/schema/schema.go b/kyaml/yaml/schema/schema.go index 3dd857d7b..9ee592f84 100644 --- a/kyaml/yaml/schema/schema.go +++ b/kyaml/yaml/schema/schema.go @@ -22,7 +22,7 @@ func IsAssociative(schema *openapi.ResourceSchema, nodes []*yaml.RNode, infer bo } for i := range nodes { node := nodes[i] - if yaml.IsEmpty(node) { + if yaml.IsMissingOrNull(node) { continue } if node.IsAssociative() { diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index 6258fbd6f..f44aa65e2 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -33,19 +33,20 @@ func MakeNullNode() *RNode { return NewRNode(&Node{Tag: NodeTagNull}) } -// IsMissingOrNull returns true if the RNode is nil or contains and explicitly null value. +// IsMissingOrNull is true if the RNode is nil or explicitly tagged null. +// TODO: make this a method on RNode. func IsMissingOrNull(node *RNode) bool { return IsNil(node) || node.YNode().Tag == NodeTagNull } -// IsEmpty returns true if the RNode is MissingOrNull +// Deprecated. Use IsMissingOrNull instead. func IsEmpty(node *RNode) bool { return IsMissingOrNull(node) } // IsEmptyMap returns true if the RNode is an empty node or an empty map func IsEmptyMap(node *RNode) bool { - return IsEmpty(node) || IsYNodeEmptyMap(node.YNode()) + return IsMissingOrNull(node) || IsYNodeEmptyMap(node.YNode()) } // IsNil return true if the node is nil, or its underlying YNode is nil. @@ -86,7 +87,7 @@ func IsYNodeString(n *yaml.Node) bool { // GetValue returns underlying yaml.Node Value field func GetValue(node *RNode) string { - if IsEmpty(node) { + if IsMissingOrNull(node) { return "" } return node.YNode().Value @@ -378,7 +379,7 @@ const ( // GetMeta returns the ResourceMeta for an RNode func (rn *RNode) GetMeta() (ResourceMeta, error) { - if IsEmpty(rn) { + if IsMissingOrNull(rn) { return ResourceMeta{}, nil } missingMeta := true diff --git a/kyaml/yaml/types_test.go b/kyaml/yaml/types_test.go index 69d7e8572..53b687e9a 100644 --- a/kyaml/yaml/types_test.go +++ b/kyaml/yaml/types_test.go @@ -183,42 +183,25 @@ func TestIsMissingOrNull(t *testing.T) { if !IsMissingOrNull(MakeNullNode()) { t.Fatalf("input: with NullNodeTag") } -} -func TestIsEmpty(t *testing.T) { - if !IsEmpty(nil) { - t.Fatalf("input: nil") - } - - // missing value or null value - if !IsEmpty(NewRNode(nil)) { - t.Fatalf("input: nil value") - } - // not array or map - if IsEmpty(NewScalarRNode("foo")) { - t.Fatalf("input: not array or map") - } -} - -func TestIsEmpty_Arrays(t *testing.T) { node := NewListRNode() // empty array. empty array is not expected as empty - if IsEmpty(node) { + if IsMissingOrNull(node) { t.Fatalf("input: empty array") } // array with 1 item node = NewListRNode("foo") - if IsEmpty(node) { + if IsMissingOrNull(node) { t.Fatalf("input: array with 1 item") } // delete the item in array node.value.Content = nil - if IsEmpty(node) { + if IsMissingOrNull(node) { t.Fatalf("input: empty array") } } -func TestIsEmpty_Maps(t *testing.T) { +func TestIsEmptyMap(t *testing.T) { node := NewMapRNode(nil) // empty map if !IsEmptyMap(node) { diff --git a/kyaml/yaml/walk/associative_sequence.go b/kyaml/yaml/walk/associative_sequence.go index 1c709cb77..120e5d224 100644 --- a/kyaml/yaml/walk/associative_sequence.go +++ b/kyaml/yaml/walk/associative_sequence.go @@ -51,7 +51,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) { if err != nil { return nil, err } - if yaml.IsEmpty(val) || yaml.IsEmptyMap(val) { + if yaml.IsMissingOrNull(val) || yaml.IsEmptyMap(val) { _, err = dest.Pipe(yaml.ElementSetter{Key: key, Value: value}) if err != nil { return nil, err @@ -74,7 +74,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) { } } // field is empty - if yaml.IsEmpty(dest) { + if yaml.IsMissingOrNull(dest) { return nil, nil } return dest, nil @@ -97,7 +97,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) { if err != nil { return nil, err } - if yaml.IsEmpty(val) { + if yaml.IsMissingOrNull(val) { _, err = dest.Pipe(yaml.ElementSetter{Key: key, Value: value}) if err != nil { return nil, err @@ -121,7 +121,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) { } // field is empty - if yaml.IsEmpty(dest) { + if yaml.IsMissingOrNull(dest) { return nil, nil } return dest, nil diff --git a/kyaml/yaml/walk/map.go b/kyaml/yaml/walk/map.go index 4300205ac..c2fb84e32 100644 --- a/kyaml/yaml/walk/map.go +++ b/kyaml/yaml/walk/map.go @@ -39,7 +39,7 @@ func (l Walker) walkMap() (*yaml.RNode, error) { continue } field := l.Sources[i].Field(key) - if field == nil || yaml.IsEmpty(field.Key) { + if field == nil || yaml.IsMissingOrNull(field.Key) { keys = append(keys, nil) continue } @@ -148,7 +148,7 @@ func (l Walker) fieldValue(fieldName string) ([]*yaml.RNode, *openapi.ResourceSc field := l.Sources[i].Field(fieldName) f, s := l.valueIfPresent(field) fields = append(fields, f) - if sch == nil && !s.IsEmpty() { + if sch == nil && !s.IsMissingOrNull() { sch = s } } diff --git a/kyaml/yaml/walk/walk.go b/kyaml/yaml/walk/walk.go index c615bc382..3bcbe9521 100644 --- a/kyaml/yaml/walk/walk.go +++ b/kyaml/yaml/walk/walk.go @@ -40,7 +40,7 @@ type Walker struct { func (l Walker) Kind() yaml.Kind { for _, s := range l.Sources { - if !yaml.IsEmpty(s) { + if !yaml.IsMissingOrNull(s) { return s.YNode().Kind } } @@ -83,7 +83,7 @@ func (l Walker) Walk() (*yaml.RNode, error) { func (l Walker) GetSchema() *openapi.ResourceSchema { for i := range l.Sources { r := l.Sources[i] - if yaml.IsEmpty(r) { + if yaml.IsMissingOrNull(r) { continue } @@ -106,7 +106,7 @@ func (l Walker) GetSchema() *openapi.ResourceSchema { } for i := range l.Sources { r := l.Sources[i] - if yaml.IsEmpty(r) { + if yaml.IsMissingOrNull(r) { continue }