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

Question - Write Remain #580

Open
chriskolenko opened this issue Jan 20, 2023 · 1 comment
Open

Question - Write Remain #580

chriskolenko opened this issue Jan 20, 2023 · 1 comment

Comments

@chriskolenko
Copy link

I'm trying to dynamically create some HCL files from golang.

I'm trying to write the following objects

type Modules struct {
	Modules []*Module `hcl:"module,block"`
}

type Module struct {
	Name    string      `hcl:"name,label"`
	Source  string      `hcl:"source"`
	Options interface{} `hcl:",remain"`
}

Using the follow, I'm not able to generate the remaining attributes

func Encode(val interface{}) []byte {
	f := hclwrite.NewEmptyFile()
	gohcl.EncodeIntoBody(val, f.Body())
	return f.Bytes()
}

##Output


module "bar" {
  source = "./bar"
}

##Expected


module "bar" {
  source = "./bar"
  
  foo = "bar"
  bar = ["foo", "bar"]
}

@apparentlymart
Copy link
Member

Hi @chriskolenko,

Indeed, this is a known assymetry in hclwrite vs. the normal decoding API, and it is because the hcl.Body API is designed for schema-driven decoding and doesn't have enough information on it's own to determine what is a suitable native syntax representation of a body.

To meet this use-case you will need to drop down to the lower-level API in the hclwrite package and manipulate the body syntax tree directly, or to write a more specific struct type which enumerates exactly which attributes you want to include so gohcl can know what schema to use to encode this data.

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