Merge pull request #160 from hashicorp/b-json-empty-list

json/parser: empty list value should not flatten
This commit is contained in:
Mitchell Hashimoto 2016-10-28 19:32:40 -04:00 committed by GitHub
commit 8fa153c5b4
5 changed files with 48 additions and 12 deletions

View File

@ -274,6 +274,14 @@ func TestDecode_interface(t *testing.T) {
}, },
}, },
{
"structure_list_empty.json",
false,
map[string]interface{}{
"foo": []interface{}{},
},
},
{ {
"nested_block_comment.hcl", "nested_block_comment.hcl",
false, false,

View File

@ -48,6 +48,12 @@ func flattenListType(
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 is empty, keep the original list
if len(ot.List) == 0 {
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 { for _, subitem := range ot.List {
if _, ok := subitem.(*ast.ObjectType); !ok { if _, ok := subitem.(*ast.ObjectType); !ok {

View File

@ -86,6 +86,7 @@ func (p *Parser) objectList() (*ast.ObjectList, error) {
break break
} }
} }
return node, nil return node, nil
} }

View File

@ -186,13 +186,13 @@ func TestFlattenObjects(t *testing.T) {
}{ }{
{ {
`{ `{
"foo": [ "foo": [
{ {
"foo": "svh", "foo": "svh",
"bar": "fatih" "bar": "fatih"
} }
] ]
}`, }`,
[]ast.Node{ []ast.Node{
&ast.ObjectType{}, &ast.ObjectType{},
&ast.LiteralType{}, &ast.LiteralType{},
@ -202,15 +202,33 @@ func TestFlattenObjects(t *testing.T) {
}, },
{ {
`{ `{
"variable": { "variable": {
"foo": {} "foo": {}
} }
}`, }`,
[]ast.Node{ []ast.Node{
&ast.ObjectType{}, &ast.ObjectType{},
}, },
1, 1,
}, },
{
`{
"empty": []
}`,
[]ast.Node{
&ast.ListType{},
},
1,
},
{
`{
"basic": [1, 2, 3]
}`,
[]ast.Node{
&ast.ListType{},
},
1,
},
} }
for _, l := range literals { for _, l := range literals {
@ -360,7 +378,7 @@ func TestParse_inline(t *testing.T) {
func equals(tb testing.TB, exp, act interface{}) { func equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) { if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act) fmt.Printf("\033[31m%s:%d:\n\n\texp: %s\n\n\tgot: %s\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow() tb.FailNow()
} }
} }

View File

@ -0,0 +1,3 @@
{
"foo": []
}