hcl icon indicating copy to clipboard operation
hcl copied to clipboard

Updating V1 to V2.6.0 - Decode issues

Open ranash-dome9 opened this issue 5 years ago • 1 comments

Hey HashiPeople,

I'm currently using V1 for decoding *.tf files to JSON using hcl.Decode as such:

func parseHcl(terraformFile string) (interface{}, error) {
	var decodedFile interface{}
	decodeError := hcl.Decode(&decodedFile, terraformFile)

	return decodedFile, decodeError
}

I want to update to V2.6.0 mainly to support version 0.12 and greater but I can't seem to find the parallel to that generic hcl.Decode I tried figuring out hcldec and hclgo Decode functions, but didn't succeed in understanding the additional arguments.

I'd really appreciate your help with that, Thanks a lot.

ranash-dome9 avatar Sep 03 '20 15:09 ranash-dome9

Hi @ranash-dome9,

It seems like you are primarily interested in parsing Terraform configuration, in which case unfortunately my answer is that there isn't a very good analog to what you had before: the Terraform language is making extensive use of various low-level HCL features and so correctly decoding it in a way compatible with Terraform would require replicating Terraform's own configuration loader, and possibly also integrating with various Terraform providers.

However, depending on what your underlying goal is, you might find the higher-level library terraform-config-inspect meets your needs better. This library is an implementation of a shallow subset of the Terraform language intended for implementing tools that might need to know the names of top-level objects defined in a configuration. It's maintained by the Terraform team and supports many different versions of the Terraform language, going back to Terraform v0.10. This is also the code that Terraform Registry uses to generate its indexes of which input variables, output values, etc are defined in a module.

You can see in the documentation for tfconfig.Module what level of abstraction this library is working at. If you drill down into the other types you'll see that it's largely focused on representing what's declared at the top level, and isn't aware of e.g. the contents of resource blocks which require access to the provider-specific schema to process properly.

apparentlymart avatar Apr 15 '21 22:04 apparentlymart