Skip to content

Commit

Permalink
fix: allow ambiguous keys like 'namespace' to be used as string rende…
Browse files Browse the repository at this point in the history
…rables
  • Loading branch information
robcxyz committed Dec 20, 2022
1 parent ceefe6a commit c1f1886
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions tackle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class BaseContext(BaseModel):
verbose: bool = False

input_context: Union[dict, list] = None
public_context: Union[dict, list] = None
private_context: Union[dict, list] = None
public_context: Union[dict, list] = {}
private_context: Union[dict, list] = {}
temporary_context: Union[dict, list] = None
existing_context: dict = {}
override_context: dict = Field({}, description="A dict to override inputs with.")
Expand Down
29 changes: 24 additions & 5 deletions tackle/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,33 @@ def render_string(context: 'Context', raw: str):
f"doesn't have arguments. Consider changing."
)

# Check for ambiguous globals like `namespace` tackle/issues/19
# Check for ambiguous globals like `namespace`
# TODO: tackle/issues/19 -> This is hacky af... Need to redo this with visitor
match = re.search(r'\<class \'(.+?)\'>', rendered_template)
if match:
ambiguous_key = match.group(1).split('.')[-1].lower()
if ambiguous_key in context.public_context:
rendered_template = context.public_context[ambiguous_key]
elif match.group(1) in context.existing_context:
rendered_template = context.existing_context[ambiguous_key]
if context.temporary_context and ambiguous_key in context.temporary_context:
ambiguous_key_rendered = context.temporary_context[ambiguous_key]
elif ambiguous_key in context.public_context:
ambiguous_key_rendered = context.public_context[ambiguous_key]
elif context.private_context and ambiguous_key in context.private_context:
ambiguous_key_rendered = context.private_context[ambiguous_key]
elif context.existing_context and ambiguous_key in context.existing_context:
ambiguous_key_rendered = context.existing_context[ambiguous_key]
else:
raise UnknownTemplateVariableException(
f"Unknown ambiguous key {ambiguous_key}. Tracking issue at "
f"tackle/issues/19",
context=context,
) from None
if isinstance(ambiguous_key_rendered, str):
rendered_template = (
rendered_template[: match.regs[0][0]]
+ ambiguous_key_rendered
+ rendered_template[match.regs[0][1] :]
)
else:
rendered_template = ambiguous_key_rendered

except TypeError as e:
# Raised when the wrong type is provided to a hook
Expand Down
2 changes: 2 additions & 0 deletions tests/render/fixtures/render-with-filters-output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
contract_name: Big Deal Token
project_slug: Big_Deal_Token
3 changes: 3 additions & 0 deletions tests/render/fixtures/render-with-filters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

contract_name: Big Deal Token
project_slug->: var contract_name|replace(' ','_')|replace('-','_')|replace('.','_')|trim()
10 changes: 6 additions & 4 deletions tests/render/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# https://github.com/robcxyz/tackle/issues/19
({'dict': {'stuff': 'things'}}, '{{dict}}', {'stuff': 'things'}),
({'namespace': {'stuff': 'things'}}, '{{namespace}}', {'stuff': 'things'}),
({'namespace': 'foo'}, '{{namespace}}bar', 'foobar'),
# Normal
({'adict': {'stuff': 'things'}}, '{{adict}}', {'stuff': 'things'}),
({'list': ['stuff', 'things']}, '{{list}}', ['stuff', 'things']),
Expand All @@ -20,9 +21,9 @@
]


@pytest.mark.parametrize("context,raw,expected_output", RENDERABLES)
def test_render_string_inputs(context, raw, expected_output):
context = Context(public_context=context)
@pytest.mark.parametrize("input_context,raw,expected_output", RENDERABLES)
def test_render_string_inputs(input_context, raw, expected_output):
context = Context(public_context=input_context)
output = render_string(context=context, raw=raw)

assert output == expected_output
Expand All @@ -32,13 +33,14 @@ def test_render_string_inputs(context, raw, expected_output):
# TODO: Not asserting - RM
# ('multi-line-block.yaml', 'multi-line-block-output.yaml'),
('call-function.yaml', 'call-function-output.yaml'),
('render-with-filters.yaml', 'render-with-filters-output.yaml'),
]


@pytest.mark.parametrize("file,expected_output", FILES)
def test_render_files(change_curdir_fixtures, file, expected_output):
o = tackle(file)
expected_output = read_config_file(expected_output)
expected_output = dict(read_config_file(expected_output))
assert o == expected_output


Expand Down

0 comments on commit c1f1886

Please sign in to comment.