Merge pull request #2822 from monopole/punchDrunk

IsFieldEmpty renamed to MapNode.IsNilOrEmpty
This commit is contained in:
Jeff Regan
2020-08-10 10:46:39 -07:00
committed by GitHub
5 changed files with 67 additions and 26 deletions

View File

@@ -238,10 +238,10 @@ func ownerToString(node *yaml.RNode) (string, error) {
owner := elements[0] owner := elements[0]
var kind, name string var kind, name string
if value := owner.Field("kind"); !yaml.IsFieldEmpty(value) { if value := owner.Field("kind"); !value.IsNilOrEmpty() {
kind = value.Value.YNode().Value kind = value.Value.YNode().Value
} }
if value := owner.Field("name"); !yaml.IsFieldEmpty(value) { if value := owner.Field("name"); !value.IsNilOrEmpty() {
name = value.Value.YNode().Value name = value.Value.YNode().Value
} }

View File

@@ -84,7 +84,7 @@ func AddSchemaFromFileUsingField(path, field string) error {
if field != "" { if field != "" {
// get the field containing the openAPI // get the field containing the openAPI
m := y.Field(field) m := y.Field(field)
if yaml.IsFieldEmpty(m) { if m.IsNilOrEmpty() {
// doesn't contain openAPI definitions // doesn't contain openAPI definitions
return nil return nil
} }

View File

@@ -568,7 +568,7 @@ var nodeTypeIndex = map[yaml.Kind]string{
} }
func ErrorIfInvalid(rn *RNode, kind yaml.Kind) error { func ErrorIfInvalid(rn *RNode, kind yaml.Kind) error {
if rn == nil || rn.YNode() == nil || rn.IsTaggedNull() { if IsMissingOrNull(rn) {
// node has no type, pass validation // node has no type, pass validation
return nil return nil
} }

View File

@@ -39,12 +39,8 @@ func IsMissingOrNull(node *RNode) bool {
return node.IsNil() || node.YNode().Tag == NodeTagNull return node.IsNil() || node.YNode().Tag == NodeTagNull
} }
// Deprecated. Use IsMissingOrNull instead. // IsEmptyMap returns true if the RNode is an empty node or an empty map.
func IsEmpty(node *RNode) bool { // TODO: make this a method on RNode.
return IsMissingOrNull(node)
}
// IsEmptyMap returns true if the RNode is an empty node or an empty map
func IsEmptyMap(node *RNode) bool { func IsEmptyMap(node *RNode) bool {
return IsMissingOrNull(node) || IsYNodeEmptyMap(node.YNode()) return IsMissingOrNull(node) || IsYNodeEmptyMap(node.YNode())
} }
@@ -89,11 +85,6 @@ func GetValue(node *RNode) string {
return node.YNode().Value return node.YNode().Value
} }
func IsFieldNull(node *MapNode) bool {
return node != nil && node.Value != nil && node.Value.YNode() != nil &&
node.Value.YNode().Tag == NodeTagNull
}
// Parser parses values into configuration. // Parser parses values into configuration.
type Parser struct { type Parser struct {
Kind string `yaml:"kind,omitempty"` Kind string `yaml:"kind,omitempty"`
@@ -269,6 +260,12 @@ type MapNode struct {
Value *RNode Value *RNode
} }
// IsNilOrEmpty returns true if the MapNode is nil,
// has no value, or has a value that appears empty.
func (mn *MapNode) IsNilOrEmpty() bool {
return mn == nil || mn.Value.IsNilOrEmpty()
}
type MapNodeSlice []*MapNode type MapNodeSlice []*MapNode
func (m MapNodeSlice) Keys() []*RNode { func (m MapNodeSlice) Keys() []*RNode {
@@ -409,17 +406,17 @@ func (rn *RNode) GetMeta() (ResourceMeta, error) {
m := ResourceMeta{} m := ResourceMeta{}
// TODO: consider optimizing this parsing // TODO: consider optimizing this parsing
if f := n.Field(APIVersionField); !IsFieldEmpty(f) { if f := n.Field(APIVersionField); !f.IsNilOrEmpty() {
m.APIVersion = GetValue(f.Value) m.APIVersion = GetValue(f.Value)
missingMeta = false missingMeta = false
} }
if f := n.Field(KindField); !IsFieldEmpty(f) { if f := n.Field(KindField); !f.IsNilOrEmpty() {
m.Kind = GetValue(f.Value) m.Kind = GetValue(f.Value)
missingMeta = false missingMeta = false
} }
mf := n.Field(MetadataField) mf := n.Field(MetadataField)
if IsFieldEmpty(mf) { if mf.IsNilOrEmpty() {
if missingMeta { if missingMeta {
return m, ErrMissingMetadata return m, ErrMissingMetadata
} }
@@ -427,16 +424,16 @@ func (rn *RNode) GetMeta() (ResourceMeta, error) {
} }
meta := mf.Value meta := mf.Value
if f := meta.Field(NameField); !IsFieldEmpty(f) { if f := meta.Field(NameField); !f.IsNilOrEmpty() {
m.Name = f.Value.YNode().Value m.Name = f.Value.YNode().Value
missingMeta = false missingMeta = false
} }
if f := meta.Field(NamespaceField); !IsFieldEmpty(f) { if f := meta.Field(NamespaceField); !f.IsNilOrEmpty() {
m.Namespace = GetValue(f.Value) m.Namespace = GetValue(f.Value)
missingMeta = false missingMeta = false
} }
if f := meta.Field(LabelsField); !IsFieldEmpty(f) { if f := meta.Field(LabelsField); !f.IsNilOrEmpty() {
m.Labels = map[string]string{} m.Labels = map[string]string{}
_ = f.Value.VisitFields(func(node *MapNode) error { _ = f.Value.VisitFields(func(node *MapNode) error {
m.Labels[GetValue(node.Key)] = GetValue(node.Value) m.Labels[GetValue(node.Key)] = GetValue(node.Value)
@@ -444,7 +441,7 @@ func (rn *RNode) GetMeta() (ResourceMeta, error) {
}) })
missingMeta = false missingMeta = false
} }
if f := meta.Field(AnnotationsField); !IsFieldEmpty(f) { if f := meta.Field(AnnotationsField); !f.IsNilOrEmpty() {
m.Annotations = map[string]string{} m.Annotations = map[string]string{}
_ = f.Value.VisitFields(func(node *MapNode) error { _ = f.Value.VisitFields(func(node *MapNode) error {
m.Annotations[GetValue(node.Key)] = GetValue(node.Value) m.Annotations[GetValue(node.Key)] = GetValue(node.Value)
@@ -663,7 +660,7 @@ func (rn *RNode) ElementValues(key string) ([]string, error) {
var elements []string var elements []string
for i := 0; i < len(rn.Content()); i++ { for i := 0; i < len(rn.Content()); i++ {
field := NewRNode(rn.Content()[i]).Field(key) field := NewRNode(rn.Content()[i]).Field(key)
if !IsFieldEmpty(field) { if !field.IsNilOrEmpty() {
elements = append(elements, field.Value.YNode().Value) elements = append(elements, field.Value.YNode().Value)
} }
} }

View File

@@ -239,16 +239,17 @@ func TestIsMissingOrNull(t *testing.T) {
t.Fatalf("input: with NullNodeTag") t.Fatalf("input: with NullNodeTag")
} }
node := NewListRNode()
// empty array. empty array is not expected as empty // empty array. empty array is not expected as empty
if IsMissingOrNull(node) { if IsMissingOrNull(NewListRNode()) {
t.Fatalf("input: empty array") t.Fatalf("input: empty array")
} }
// array with 1 item // array with 1 item
node = NewListRNode("foo") node := NewListRNode("foo")
if IsMissingOrNull(node) { if IsMissingOrNull(node) {
t.Fatalf("input: array with 1 item") t.Fatalf("input: array with 1 item")
} }
// delete the item in array // delete the item in array
node.value.Content = nil node.value.Content = nil
if IsMissingOrNull(node) { if IsMissingOrNull(node) {
@@ -355,3 +356,46 @@ func TestRNodeIsNilOrEmpty(t *testing.T) {
t.Fatalf("non-empty list should not be empty") t.Fatalf("non-empty list should not be empty")
} }
} }
func TestMapNodeIsNilOrEmpty(t *testing.T) {
var mn *MapNode
if !mn.IsNilOrEmpty() {
t.Fatalf("nil should be empty")
}
mn = &MapNode{Key: MakeNullNode()}
if !mn.IsNilOrEmpty() {
t.Fatalf("missing value should be empty")
}
mn.Value = NewRNode(nil)
if !mn.IsNilOrEmpty() {
t.Fatalf("missing value YNode should be empty")
}
mn.Value = MakeNullNode()
if !mn.IsNilOrEmpty() {
t.Fatalf("value tagged null should be empty")
}
mn.Value = NewMapRNode(nil)
if !mn.IsNilOrEmpty() {
t.Fatalf("empty map should be empty")
}
mn.Value = NewMapRNode(&map[string]string{"foo": "bar"})
if mn.IsNilOrEmpty() {
t.Fatalf("non-empty map should not be empty")
}
mn.Value = NewListRNode()
if !mn.IsNilOrEmpty() {
t.Fatalf("empty list should be empty")
}
mn.Value = NewListRNode("foo")
if mn.IsNilOrEmpty() {
t.Fatalf("non-empty list should not be empty")
}
}