mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 08:20:59 +00:00
code review
This commit is contained in:
@@ -28,10 +28,7 @@ func NullNode() *RNode {
|
||||
|
||||
// IsMissingOrNull returns true if the RNode is nil or contains and explicitly null value.
|
||||
func IsMissingOrNull(node *RNode) bool {
|
||||
if node == nil || node.YNode() == nil || node.YNode().Tag == NullNodeTag {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return node == nil || node.YNode() == nil || node.YNode().Tag == NullNodeTag
|
||||
}
|
||||
|
||||
// IsEmpty returns true if the RNode is MissingOrNull, or is either a MappingNode with
|
||||
@@ -44,11 +41,7 @@ func IsEmpty(node *RNode) bool {
|
||||
// Empty sequence is a special case and temporarily not considered as empty here.
|
||||
// Some users may want to keep empty sequence for compatibility reason.
|
||||
// For example, use JSON 6902 patch.
|
||||
if node.YNode().Kind == yaml.MappingNode && len(node.YNode().Content) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
return node.YNode().Kind == yaml.MappingNode && len(node.YNode().Content) == 0
|
||||
}
|
||||
|
||||
func IsNull(node *RNode) bool {
|
||||
|
||||
@@ -169,80 +169,84 @@ type: string
|
||||
}
|
||||
|
||||
func TestIsMissingOrNull(t *testing.T) {
|
||||
if IsMissingOrNull(nil) != true {
|
||||
if !IsMissingOrNull(nil) {
|
||||
t.Fatalf("input: nil")
|
||||
}
|
||||
// missing value or null value
|
||||
node := &RNode{value: nil}
|
||||
if IsMissingOrNull(node) != true {
|
||||
if !IsMissingOrNull(node) {
|
||||
t.Fatalf("input: nil value")
|
||||
}
|
||||
|
||||
node.value = &yaml.Node{}
|
||||
if IsMissingOrNull(node) != false {
|
||||
if IsMissingOrNull(node) {
|
||||
t.Fatalf("input: valid node")
|
||||
}
|
||||
// node with NullNodeTag
|
||||
node.value.Tag = NullNodeTag
|
||||
if IsMissingOrNull(node) != true {
|
||||
if !IsMissingOrNull(node) {
|
||||
t.Fatalf("input: with NullNodeTag")
|
||||
}
|
||||
|
||||
node.value = &yaml.Node{}
|
||||
if IsMissingOrNull(node) != false {
|
||||
if IsMissingOrNull(node) {
|
||||
t.Fatalf("input: valid node")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmpty(t *testing.T) {
|
||||
if IsEmpty(nil) != true {
|
||||
if !IsEmpty(nil) {
|
||||
t.Fatalf("input: nil")
|
||||
}
|
||||
|
||||
// missing value or null value
|
||||
node := &RNode{value: nil}
|
||||
if IsEmpty(node) != true {
|
||||
if !IsEmpty(node) {
|
||||
t.Fatalf("input: nil value")
|
||||
}
|
||||
// not array or map
|
||||
node.value = &yaml.Node{}
|
||||
if IsEmpty(node) != false {
|
||||
if IsEmpty(node) {
|
||||
t.Fatalf("input: not array or map")
|
||||
}
|
||||
}
|
||||
|
||||
// === array tests ===
|
||||
node.value.Kind = yaml.SequenceNode
|
||||
func TestIsEmpty_Arrays(t *testing.T) {
|
||||
node := &RNode{value: &yaml.Node{
|
||||
Kind: yaml.SequenceNode,
|
||||
}}
|
||||
// empty array. empty array is not expected as empty
|
||||
if IsEmpty(node) != false {
|
||||
if IsEmpty(node) {
|
||||
t.Fatalf("input: empty array")
|
||||
}
|
||||
// array with 1 item
|
||||
node.value.Content = append(node.value.Content, &yaml.Node{})
|
||||
if IsEmpty(node) != false {
|
||||
if IsEmpty(node) {
|
||||
t.Fatalf("input: array with 1 item")
|
||||
}
|
||||
// delete the item in array
|
||||
node.value.Content = node.value.Content[:len(node.value.Content)-1]
|
||||
if IsEmpty(node) != false {
|
||||
if IsEmpty(node) {
|
||||
t.Fatalf("input: empty array")
|
||||
}
|
||||
}
|
||||
|
||||
// === map tests ===
|
||||
node.value = &yaml.Node{
|
||||
func TestIsEmpty_Maps(t *testing.T) {
|
||||
node := &RNode{value: &yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
}
|
||||
}}
|
||||
// empty map
|
||||
if IsEmpty(node) != true {
|
||||
if !IsEmpty(node) {
|
||||
t.Fatalf("input: empty map")
|
||||
}
|
||||
// map with 1 item
|
||||
node.value.Content = append(node.value.Content, &yaml.Node{})
|
||||
if IsEmpty(node) != false {
|
||||
if IsEmpty(node) {
|
||||
t.Fatalf("input: map with 1 item")
|
||||
}
|
||||
// delete the item in map
|
||||
node.value.Content = node.value.Content[:len(node.value.Content)-1]
|
||||
if IsEmpty(node) != true {
|
||||
if !IsEmpty(node) {
|
||||
t.Fatalf("input: empty map")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user