flatten nested lists for flatten in visitor

This commit is contained in:
David Eads 2018-06-26 11:47:46 -04:00
parent 63c33f3812
commit a31d2c44f4
3 changed files with 58 additions and 4 deletions

View File

@ -376,12 +376,28 @@ func (v FlattenListVisitor) Visit(fn VisitorFunc) error {
if info.Object == nil {
return fn(info, nil)
}
items, err := meta.ExtractList(info.Object)
if err != nil {
if !meta.IsListType(info.Object) {
return fn(info, nil)
}
if errs := runtime.DecodeList(items, v.mapper.decoder); len(errs) > 0 {
return utilerrors.NewAggregate(errs)
items := []runtime.Object{}
itemsToProcess := []runtime.Object{info.Object}
for i := 0; i < len(itemsToProcess); i++ {
currObj := itemsToProcess[i]
if !meta.IsListType(currObj) {
items = append(items, currObj)
continue
}
currItems, err := meta.ExtractList(currObj)
if err != nil {
return err
}
if errs := runtime.DecodeList(currItems, v.mapper.decoder); len(errs) > 0 {
return utilerrors.NewAggregate(errs)
}
itemsToProcess = append(itemsToProcess, currItems...)
}
// If we have a GroupVersionKind on the list, prioritize that when asking for info on the objects contained in the list

View File

@ -24,6 +24,7 @@ import (
"testing"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)
@ -163,3 +164,19 @@ func TestVisitorHttpGet(t *testing.T) {
})
}
}
func TestFlattenListVisitor(t *testing.T) {
b := newDefaultBuilder().
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{"../../../../test/fixtures/pkg/kubectl/builder/deeply-nested.yaml"}}).
Flatten()
test := &testVisitor{}
err := b.Do().Visit(test.Handle)
if err != nil {
t.Fatal(err)
}
if len(test.Infos) != 6 {
t.Fatal(spew.Sdump(test.Infos))
}
}

View File

@ -0,0 +1,21 @@
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Pod
- apiVersion: v1
kind: Pod
- apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Pod
- apiVersion: v1
kind: Pod
- apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Pod
- apiVersion: v1
kind: Pod