Skip to content

Commit

Permalink
Fix block labels and booleans during reconstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
weaversam8 committed Oct 11, 2024
1 parent bfa02d0 commit 3d7ae42
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions hcl2/reconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,13 @@ def _NL(self, level: int, comma: bool = False) -> Tree:
# rules: the value of a block is always an array of dicts,
# the key is the block type
def _list_is_a_block(self, v: list) -> bool:
sub_obj = v[0]
for obj in v:
if not self._dict_is_a_block(obj):
return False

return True

def _dict_is_a_block(self, sub_obj: any) -> bool:
# if the list doesn't contain dictionaries, it's not a block
if not isinstance(sub_obj, dict):
return False
Expand All @@ -223,7 +228,7 @@ def _list_is_a_block(self, v: list) -> bool:
# object is a block (recurse)
label = list(sub_obj)[0]
sub_sub_obj = sub_obj[label]
if self._list_is_a_block([sub_sub_obj]):
if self._dict_is_a_block(sub_sub_obj):
return True

# if the objects in the array have a single key whose child is not a
Expand All @@ -234,6 +239,11 @@ def _block_has_label(self, b: dict) -> bool:
return len(b.keys()) == 1

def _calculate_block_labels(self, b: dict) -> List[str]:
# if b doesn't have a label
if len(b.keys()) != 1:
return ([], b)

# otherwise, find the label
curr_label = list(b)[0]
potential_body = b[curr_label]
if (
Expand Down Expand Up @@ -351,6 +361,17 @@ def _transform_value_to_expr_term(self, v, level) -> Token:
return Tree(
Token("RULE", "expr_term"), [Tree(Token("RULE", "object"), elems)]
)
# treat booleans appropriately
elif isinstance(v, bool):
return Tree(
Token("RULE", "expr_term"),
[
Tree(
Token("RULE", "identifier"),
[Token("NAME", "true" if v else "false")],
)
],
)
# store integers as literals, digit by digit
elif isinstance(v, int):
return Tree(
Expand Down

0 comments on commit 3d7ae42

Please sign in to comment.