Issue parsing HCL object within a block
Hey, I am trying to parse terraform HCL into Golang struct and im facing issues when trying to parse an object inside a Module block. this it the example I am trying to make work: Terraform file:
module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-s3-bucket"
versioning = {
enabled = true
}
}
Golang struct:
type Attributes struct {
Source string `hcl:"source,attr"`
Bucket string `hcl:"bucket,attr"`
Versioning Versioning `hcl:"versioning,attr"`
}
type Versioning struct {
Enabled bool `hcl:"enabled,attr"`
}
I am being able to parse both of source and bucket attributes, however, no matter what I try I was not able to parse the versioning attribute.
Hi @raz-bn,
The "hcl" struct tags only interact with HCL blocks and their direct attributes. From HCL's perspective that versioning argument is just a single expression evaluated all at once, so HCL does not destructure it.
gohcl handles each attribute by passing it to gohcl.DecodeExpression, which in turn delegates to a third-party package gocty to convert to the final type. Therefore you would need to use that package's struct tags for your type Versioning:
type Versioning struct {
Enabled bool `cty:"enabled"`
}