mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-25 08:17:02 +00:00
Merge pull request #2820 from monopole/dropIsEmptyCalls
Replace all calls to yaml.IsEmpty with IsMissingOrNull
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user