Fix flattening of empty objects
This commit is contained in:
parent
5f90c29834
commit
cf83c24abf
@ -76,6 +76,12 @@ func flattenObjectType(
|
|||||||
item *ast.ObjectItem,
|
item *ast.ObjectItem,
|
||||||
items []*ast.ObjectItem,
|
items []*ast.ObjectItem,
|
||||||
frontier []*ast.ObjectItem) ([]*ast.ObjectItem, []*ast.ObjectItem) {
|
frontier []*ast.ObjectItem) ([]*ast.ObjectItem, []*ast.ObjectItem) {
|
||||||
|
// If the list has no items we do not have to flatten anything
|
||||||
|
if ot.List.Items == nil {
|
||||||
|
items = append(items, item)
|
||||||
|
return items, frontier
|
||||||
|
}
|
||||||
|
|
||||||
// All the elements of this object must also be objects!
|
// All the elements of this object must also be objects!
|
||||||
for _, subitem := range ot.List.Items {
|
for _, subitem := range ot.List.Items {
|
||||||
if _, ok := subitem.Val.(*ast.ObjectType); !ok {
|
if _, ok := subitem.Val.(*ast.ObjectType); !ok {
|
||||||
|
@ -182,6 +182,7 @@ func TestFlattenObjects(t *testing.T) {
|
|||||||
var literals = []struct {
|
var literals = []struct {
|
||||||
src string
|
src string
|
||||||
nodeType []ast.Node
|
nodeType []ast.Node
|
||||||
|
itemLen int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
`{
|
`{
|
||||||
@ -196,8 +197,19 @@ func TestFlattenObjects(t *testing.T) {
|
|||||||
&ast.ObjectType{},
|
&ast.ObjectType{},
|
||||||
&ast.LiteralType{},
|
&ast.LiteralType{},
|
||||||
&ast.LiteralType{},
|
&ast.LiteralType{},
|
||||||
&ast.ListType{},
|
|
||||||
},
|
},
|
||||||
|
3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`{
|
||||||
|
"variable": {
|
||||||
|
"foo": {}
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
[]ast.Node{
|
||||||
|
&ast.ObjectType{},
|
||||||
|
},
|
||||||
|
1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,6 +241,10 @@ func TestFlattenObjects(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the number of items is correct
|
||||||
|
equals(t, l.itemLen, i)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user