Skip to content
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

allow finding functions expression depends on #456

Open
tonistiigi opened this issue Mar 27, 2021 · 2 comments
Open

allow finding functions expression depends on #456

tonistiigi opened this issue Mar 27, 2021 · 2 comments

Comments

@tonistiigi
Copy link

Currently Expression https://pkg.go.dev/github.com/hashicorp/hcl/v2#Expression provides access to variables it uses via Variables() method. This can be used for dynamic lookup for the EvalContext. But similar concept is not provided for functions.

Currently, I can do

// exp.(hclsyntax.Node)
hclsyntax.VisitAll(node, func(n hclsyntax.Node) hcl.Diagnostics {
  if fe, ok := n.(*hclsyntax.FunctionCallExpr); ok {
    ...
  }
  return nil
})

what isn't too awful.

But that does not work when parsing JSON inputs where *json.expression does not provide such access. Atm. the only solution I found for that case was doing some ugly hacks with the reflect package. There is smth called ExprCall but afaics that only works on a very specific input of a single function call.

There should be some common pattern to achieve this. Eg. maybe add Functions() []string method tho hcl.Expression. Or improve ways for walking through JSON expression types. I'm willing to contribute this if we can agree on the design first.

@apparentlymart
Copy link
Contributor

Hi @tonistiigi,

Indeed, at the moment the assumption is that your table of functions will be static for all expressions, rather than dynamic based on what the expression is calling. The ability to look up variable references to populate a dynamic scope arose from Terraform's need to build a dependency graph; I'd be curious to hear about what you are doing that calls for static analysis of function calls!

@tonistiigi
Copy link
Author

I'm using this in docker/buildx#575 . The code is quite ugly atm, I hope to refactor it into a higher-level parser function that supports vars, user-funcs and cross-referencing values by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants