Skip to content

Commit

Permalink
address "escaped quotes" comment from #57
Browse files Browse the repository at this point in the history
  • Loading branch information
ITProKyle committed Jan 7, 2020
1 parent 52fa4a9 commit 3f24c92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
35 changes: 22 additions & 13 deletions src/hcl/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ class HclParser(object):
def objectlist_flat(self, lt, replace):
'''
Similar to the dict constructor, but handles dups
HCL is unclear on what one should do when duplicate keys are
encountered. These comments aren't clear either:
from decoder.go: if we're at the root or we're directly within
a list, decode into dicts, otherwise lists
from object.go: there's a flattened list structure
'''
d = {}
Expand Down Expand Up @@ -379,20 +379,29 @@ def p_function_3(self, p):
)

def flatten(self, value):
returnValue = ""
if type(value) is dict:
returnValue = (
if isinstance(value, dict):
return (
"{"
+ ",".join(key + ":" + self.flatten(value[key]) for key in value)
+ "}"
)
elif type(value) is list:
returnValue = ",".join(self.flatten(v) for v in value)
elif type(value) is tuple:
returnValue = " ".join(self.flatten(v) for v in value)
else:
returnValue = value
return returnValue
if isinstance(value, list):
return ",".join(self.flatten(v) for v in value)
if isinstance(value, tuple):
return " ".join(self.flatten(v) for v in value)
if isinstance(value, str):
if value.isnumeric(): # return numbers as is
return value
return (
'"' + value + '"' # wrap string literals in double quotes
if value not in ['+', '-'] and '.' not in value
else value # but not if its var interpolation or an operator
)
raise TypeError(
'%s is of type %s; expected type of dict, list, tuple, or str',
str(value),
type(value),
)

def p_listitems_0(self, p):
'''
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"resource": {
"object": {
"vars": {
"cluster_1": "join('\\n',data.template_file.cluster_1.*.rendered)",
"cluster_2": "format(name_%02d,count.index + 1)",
"PROXY_AUTH": "join(':',var.proxy_username,var.proxy_password)"
"cluster_1": "join(\"\\n\",data.template_file.cluster_1.*.rendered)",
"cluster_2": "format(\"name_%02d\",count.index + 1)",
"PROXY_AUTH": "join(\":\",var.proxy_username,var.proxy_password)"
}
}
}
}
}
}
1 change: 1 addition & 0 deletions tests/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
('flat.hcl', None, {'foo': 'bar', 'Key': 7}),
('float.hcl', None, {'a': 1.02}),
('float.hcl', 'float.json', None),
('function.hcl', 'function.json', None),
('multiline_bad.hcl', 'multiline.json', None),
('scientific.hcl', 'scientific.json', None),
('structure.hcl', 'structure_flat.json', None),
Expand Down

0 comments on commit 3f24c92

Please sign in to comment.