Skip to content

adds support for provider functions #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
| index_expr_term
| get_attr_expr_term
| identifier
| provider_function_call
| heredoc_template
| heredoc_template_trim
| attr_splat_expr_term
Expand Down Expand Up @@ -55,6 +56,8 @@ heredoc_template_trim : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*

function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")"
arguments : (expression (new_line_or_comment* "," new_line_or_comment* expression)* ("," | "...")? new_line_or_comment*)
colons: "::"
Copy link
Collaborator

@kkozik-amplify kkozik-amplify Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this definition and use :: directly in provider_function_call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont get it done, everytime I inline "::" I get this error:

FAIL: test_load_terraform_from_cache:10
'provider_function.tf' (test.unit.test_load.TestLoad.test_load_terraform_from_cache:10
'provider_function.tf')
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../python-hcl2/.tox/py312-unit/lib/python3.12/site-packages/lark/visitors.py", line 124, in _call_userfunc
    return f(children)
           ^^^^^^^^^^^
TypeError: sequence item 1: expected str instance, Tree found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../python-hcl2/test/unit/test_load.py", line 43, in check_terraform
    hcl2_dict = hcl2.load(hcl_file)
                ^^^^^^^^^^^^^^^^^^^
lark.exceptions.VisitError: Error trying to process rule "provider_function_call":

sequence item 1: expected str instance, Tree found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../python-hcl2/test/unit/test_load.py", line 45, in check_terraform
    assert False, f"failed to tokenize terraform in `{hcl_path_str}`: {exc}"
           ^^^^^
AssertionError: failed to tokenize terraform in `provider_function.tf`: Error trying to process rule "provider_function_call":

sequence item 1: expected str instance, Tree found

I'm not so fluent, maybe someone could assist?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into that and this happens because of how the provider_function_call works. This function is consistent with function_call you seemed to base on so let's stick to the implementation you proposed.
Thanks!

provider_function_call: identifier colons identifier colons identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")"

index_expr_term : expr_term index
get_attr_expr_term : expr_term get_attr
Expand Down
8 changes: 8 additions & 0 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ def function_call(self, args: List) -> str:
args_str = ", ".join([str(arg) for arg in args[1] if arg is not Discard])
return f"{args[0]}({args_str})"

def provider_function_call(self, args: List) -> str:
args = self.strip_new_line_tokens(args)
args_str = ""
if len(args) > 5:
args_str = ", ".join([str(arg) for arg in args[5] if arg is not Discard])
provider_func = "::".join([args[0],args[2],args[4]])
return f"{provider_func}({args_str})"

def arguments(self, args: List) -> List:
return args

Expand Down
8 changes: 8 additions & 0 deletions test/helpers/terraform-config-json/provider_function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"locals": [
{
"name2": "${provider::test2::test(\"a\")}",
"name3": "${test(\"a\")}"
}
]
}
4 changes: 4 additions & 0 deletions test/helpers/terraform-config/provider_function.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
locals {
name2 = provider::test2::test("a")
name3 = test("a")
}
Loading