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

Read HCL file without evaluation. #585

Open
kamilpi opened this issue Jan 30, 2023 · 2 comments
Open

Read HCL file without evaluation. #585

kamilpi opened this issue Jan 30, 2023 · 2 comments

Comments

@kamilpi
Copy link

kamilpi commented Jan 30, 2023

Hello,

I want to read terraform/terragrunt configuration files, store them inside of my stricture in golang test and use them for any purpose like generating new files with a different configuration, and do the validation if this file suits the current version of tests.

Right now when I try to read the HCL file when there is something like a "merge" function(terraform) I receive an error like this:

Function calls not allowed; Functions may not be called here.

Is there any way to read the such file without evaluation?

@mlund01
Copy link

mlund01 commented Mar 2, 2023

Hi @normander,

First, the reason for the error you received is probably due to having a nil EvalContext when you are decoding your config. You can read more on that here, but importantly, if you want to be able to evaluate those functions, you will need to provide them in the EvalContext yourself. If you are wondering where to find these functions, check out cty stdlib functions

Second, to answer your question on reading a file without evaluation - yes, you can decode your hcl config into Native Go values without evaluation. You can do so by utilizing hcl.Expression or hcl.Body types in your Go struct definitions. For example,

type Data struct {
    Name   string `hcl:"name,label"`
    Field1 hcl.Expression `hcl:"field1,attr"`
    Remain hcl.Body `hcl:",remain"`
}

When you decode your config, the resulting Data variable will have Field1 stored as an unevaluated expression, of which you can evaluate later with methods on hcl.Expression. The Remain field will store "everything else" from your config that wasn't defined explicitly in the struct, and can be decoded in a separate step into a different struct at a later time.

Note that a common approach when needing to leave certain fields unevaluated is to accomplish it in a stepped fashion. Start by defining a struct with "just enough" of the fields you absolutely need to decode into primitive go values, leave the rest as hcl.Expression (for fields you want to define explicitly in the step but leave unevaluated) and hcl.Body as a Remain field (note how the hcl tag is defined. That's important) for all the config values you will worry about later.

Here is a good answer to a similar question on decoding but not evaluating config until later... #496 (comment)

@catdevman
Copy link

@kamilpi did you get the answer you're looking for?

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

3 participants