diff --git a/src/hcl/parser.py b/src/hcl/parser.py index d6a9121..4612ee5 100644 --- a/src/hcl/parser.py +++ b/src/hcl/parser.py @@ -71,17 +71,20 @@ def objectlist_flat(self, lt, replace): else: if isinstance(v, dict): dd = d.setdefault(k, {}) - for kk, vv in iteritems(v): - if type(dd) == list: - dd.append({kk: vv}) - elif kk in dd.keys(): - if hasattr(vv, 'items'): - for k2, v2 in iteritems(vv): - dd[kk][k2] = v2 + if type(dd) is list: + dd.append(v) + else: + for kk, vv in iteritems(v): + if kk in dd.keys(): + if hasattr(vv, 'items'): + for k2, v2 in iteritems(vv): + dd[kk][k2] = v2 + else: + newdd = {kk: vv} + d[k] = [dd, newdd] + dd = newdd else: - d[k] = [dd, {kk: vv}] - else: - dd[kk] = vv + dd[kk] = vv else: d[k] = v diff --git a/tests/fixtures/structure_list2.hcl b/tests/fixtures/structure_list2.hcl new file mode 100644 index 0000000..1578277 --- /dev/null +++ b/tests/fixtures/structure_list2.hcl @@ -0,0 +1,8 @@ +top { + a = "a" + b = "b" +} +top { + b = "b" + c = "c" +} diff --git a/tests/fixtures/structure_list2.json b/tests/fixtures/structure_list2.json new file mode 100644 index 0000000..261f077 --- /dev/null +++ b/tests/fixtures/structure_list2.json @@ -0,0 +1,12 @@ +{ + "top": [ + { + "a": "a", + "b": "b" + }, + { + "b": "b", + "c": "c" + } + ] +} diff --git a/tests/fixtures/structure_list3.hcl b/tests/fixtures/structure_list3.hcl new file mode 100644 index 0000000..1245e56 --- /dev/null +++ b/tests/fixtures/structure_list3.hcl @@ -0,0 +1,7 @@ +top { + a = "a" + b = "b" +} +top { + c = "c" +} diff --git a/tests/fixtures/structure_list3.json b/tests/fixtures/structure_list3.json new file mode 100644 index 0000000..ea34330 --- /dev/null +++ b/tests/fixtures/structure_list3.json @@ -0,0 +1,11 @@ +{ + "top": [ + { + "a": "a", + "b": "b" + }, + { + "c": "c" + } + ] +} diff --git a/tests/test_decoder.py b/tests/test_decoder.py index aafcf3e..01dd81c 100644 --- a/tests/test_decoder.py +++ b/tests/test_decoder.py @@ -29,6 +29,8 @@ ('structure_flatmap.hcl', 'structure_flatmap.json', None), ('structure_list.hcl', 'structure_list.json', None), # these don't match in golang either ('structure_list.hcl', None, {'foo': [{'key': 7}, {'key': 12}]}), # nor this + ('structure_list2.hcl', 'structure_list2.json', None), + ('structure_list3.hcl', 'structure_list3.json', None), ('issue12.hcl', 'issue12.json', None), #'structure_list_deep.json' ('structure_multi.hcl', 'structure_multi.json', None),