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