rodaine / hclencoder

HCL Encoder/Marshaller - Convert Go Types into HCL files
https://godoc.org/github.com/rodaine/hclencoder
MIT License
77 stars 32 forks source link

Maps as attribute #17

Closed vynaloze closed 5 years ago

vynaloze commented 5 years ago

Hi! I think there may be a bug while encoding maps:

type test struct {
  MapAttr map[string]string `hcl:"MapAttr"`
}

should be encoded as

MapAttr = {
  foo = "bar"
}

and currently is encoded as block:

MapAttr {
  foo = "bar"
}

Due to that, while parsing this code, official hclparse fails:

Error: Missing required argument

  on test.hcl line 1, in MapAttr:
   1: MapAttr {

The argument "MapAttr" is required, but no definition was found.

Error: Unsupported block type

  on test.hcl line 1, in MapAttr:
   1: MapAttr {

Blocks of type "MapAttr" are not expected here. Did you mean to define
argument "MapAttr"? If so, use the equals sign to assign it a value.

Try it yourself: https://play.golang.org/p/4xShKnfAXa1

I've added a failing test which covers this functionality on my fork, I didn't look into implementation yet.

What do you think about this issue?

vynaloze commented 5 years ago

Okay, this is caused because I use new hcl2 instead of hcl. Should've checked it before.

gwax commented 5 years ago

@vynaloze did you find a solution?

I too would like to use hclencoder with terraform 0.12 but am running into this exact issue.

vynaloze commented 5 years ago

I use official hcl write package: https://godoc.org/github.com/hashicorp/hcl2/hclwrite

It's pretty low-level and the documentation is not great - but it gets work done.