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

Issue parsing HCL object within a block #672

Open
raz-bn opened this issue Apr 7, 2024 · 1 comment
Open

Issue parsing HCL object within a block #672

raz-bn opened this issue Apr 7, 2024 · 1 comment

Comments

@raz-bn
Copy link

raz-bn commented Apr 7, 2024

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.

@apparentlymart
Copy link
Contributor

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"`
}

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