Merge pull request #2855 from phanimarupaka/DeleteSubst

Delete substitution and fix delete setters
This commit is contained in:
Kubernetes Prow Robot
2020-08-19 12:27:23 -07:00
committed by GitHub
12 changed files with 799 additions and 169 deletions

View File

@@ -384,6 +384,23 @@ func (rn *RNode) Fields() ([]string, error) {
return fields, nil
}
// FieldRNodes returns the list of field key RNodes for a MappingNode.
// Returns an error for non-MappingNodes.
func (rn *RNode) FieldRNodes() ([]*RNode, error) {
if err := ErrorIfInvalid(rn, yaml.MappingNode); err != nil {
return nil, errors.Wrap(err)
}
var fields []*RNode
for i := 0; i < len(rn.Content()); i += 2 {
yNode := rn.Content()[i]
// for each key node in the input mapping node contents create equivalent rNode
rNode := &RNode{}
rNode.SetYNode(yNode)
fields = append(fields, rNode)
}
return fields, nil
}
// Field returns a fieldName, fieldValue pair for MappingNodes.
// Returns nil for non-MappingNodes.
func (rn *RNode) Field(field string) *MapNode {

View File

@@ -206,6 +206,62 @@ func TestIsMissingOrNull(t *testing.T) {
}
}
func TestFieldRNodes(t *testing.T) {
testCases := []struct {
testName string
input string
output []string
err string
}{
{
testName: "simple document",
input: `apiVersion: example.com/v1beta1
kind: Example1
spec:
list:
- "a"
- "b"
- "c"`,
output: []string{"apiVersion", "kind", "spec", "list"},
},
{
testName: "non mapping node error",
input: `apiVersion`,
err: "wrong Node Kind for expected: MappingNode was ScalarNode: value: {apiVersion}",
},
}
for i := range testCases {
tc := testCases[i]
t.Run(tc.testName, func(t *testing.T) {
rNode, err := Parse(tc.input)
if !assert.NoError(t, err) {
t.FailNow()
}
fieldRNodes, err := rNode.FieldRNodes()
if tc.err == "" {
if !assert.NoError(t, err) {
t.FailNow()
}
} else {
if !assert.Equal(t, tc.err, err.Error()) {
t.FailNow()
}
}
for i := range fieldRNodes {
actual, err := fieldRNodes[i].String()
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.Equal(t, tc.output[i], strings.TrimSpace(actual)) {
t.FailNow()
}
}
})
}
}
func TestIsEmptyMap(t *testing.T) {
node := NewMapRNode(nil)
// empty map