mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Add function to check is YNode zero
This commit is contained in:
@@ -197,7 +197,8 @@ func (rn *RNode) IsNilOrEmpty() bool {
|
|||||||
return rn.IsNil() ||
|
return rn.IsNil() ||
|
||||||
IsYNodeTaggedNull(rn.YNode()) ||
|
IsYNodeTaggedNull(rn.YNode()) ||
|
||||||
IsYNodeEmptyMap(rn.YNode()) ||
|
IsYNodeEmptyMap(rn.YNode()) ||
|
||||||
IsYNodeEmptySeq(rn.YNode())
|
IsYNodeEmptySeq(rn.YNode()) ||
|
||||||
|
IsYNodeZero(rn.YNode())
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMeta returns the ResourceMeta for an RNode
|
// GetMeta returns the ResourceMeta for an RNode
|
||||||
|
|||||||
@@ -750,6 +750,10 @@ func TestRNodeIsNilOrEmpty(t *testing.T) {
|
|||||||
if NewListRNode("foo").IsNilOrEmpty() {
|
if NewListRNode("foo").IsNilOrEmpty() {
|
||||||
t.Fatalf("non-empty list should not be empty")
|
t.Fatalf("non-empty list should not be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !NewRNode(&Node{}).IsNilOrEmpty() {
|
||||||
|
t.Fatalf("zero YNode should be empty")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const deploymentJSON = `
|
const deploymentJSON = `
|
||||||
|
|||||||
@@ -54,6 +54,18 @@ func IsYNodeString(n *yaml.Node) bool {
|
|||||||
return n.Kind == yaml.ScalarNode && n.Tag == NodeTagString
|
return n.Kind == yaml.ScalarNode && n.Tag == NodeTagString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsYNodeZero is true if all the public fields in the Node are empty.
|
||||||
|
// Which means it's not initialized and should be omitted when marshal.
|
||||||
|
// The Node itself has a method IsZero but it is not released
|
||||||
|
// in yaml.v3. https://pkg.go.dev/gopkg.in/yaml.v3#Node.IsZero
|
||||||
|
func IsYNodeZero(n *yaml.Node) bool {
|
||||||
|
// TODO: Change this to use IsZero when it's avaialable.
|
||||||
|
return n != nil && n.Kind == 0 && n.Style == 0 && n.Tag == "" && n.Value == "" &&
|
||||||
|
n.Anchor == "" && n.Alias == nil && n.Content == nil &&
|
||||||
|
n.HeadComment == "" && n.LineComment == "" && n.FootComment == "" &&
|
||||||
|
n.Line == 0 && n.Column == 0
|
||||||
|
}
|
||||||
|
|
||||||
// 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"`
|
||||||
|
|||||||
@@ -112,3 +112,15 @@ func TestIsYNodeEmptySeq(t *testing.T) {
|
|||||||
t.Fatalf("a node with content isn't empty")
|
t.Fatalf("a node with content isn't empty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsYNodeZero(t *testing.T) {
|
||||||
|
if IsYNodeZero(nil) {
|
||||||
|
t.Fatalf("nil node should not be zero")
|
||||||
|
}
|
||||||
|
if !IsYNodeZero(&Node{}) {
|
||||||
|
t.Fatalf("node is zero")
|
||||||
|
}
|
||||||
|
if IsYNodeZero(&Node{Kind: MappingNode}) {
|
||||||
|
t.Fatalf("node is not zero")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user