Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Distinguishing attributes vs blocks #18

Open
jamesmharvey opened this issue Aug 14, 2019 · 1 comment
Open

Distinguishing attributes vs blocks #18

jamesmharvey opened this issue Aug 14, 2019 · 1 comment

Comments

@jamesmharvey
Copy link

I'd like to use hclencoder with Terraform 0.12, but this version of terraform is a lot pickier about attributes vs blocks.

Due to the design of the configuration language decoder in Terraform v0.11 and earlier, it was in many cases possible to interchange the argument syntax (with =) and the block syntax (with just braces) when dealing with map arguments vs. nested blocks. However, this led to some subtle bugs and limitations, so Terraform v0.12 now requires consistent usage of argument syntax for arguments and nested block syntax for nested blocks.

The current hclencoder code treats all ObjectType blocks (i.e. all maps and structs) as nested blocks, rather than as attributes being assigned:

		if _, ok := node.Val.(*ast.ObjectType); !ok {
			node.Assign = cur.pos()
		}

I'm not particularly familiar with HCL usage outside of Terraform, but at least in Terraform 0.12, it seems that maps and structs can be used either as attributes or as blocks. (I may be mistaken on this point, it may be that maps are always attributes and structs are always blocks, I've just started diving into the details of HCL in the last day or two, so please correct me if I'm wrong on this point.) It would be nice if hclencoder could distinguish between when to write an attribute vs a nested block.

To be clear, in some circumstances, the code

	type Foo struct {
		Bar map[string]string
	}
	r := Foo{
		Bar: map[string]string{
			"abc": "def",
		},
	}
	body, _ := hclencoder.Encode(r)

should produce the block

// Current behavior
Bar {
  abc = "def"
}

but in others it should produce the assignment

// Sometimes desired behavior
Bar = {
  abc = "def"
}

For reference, this is a more general version of the issue raised in #17 .

I think the best way forward here may be to add an additional hcle tag, to allow hclencoder to determine whether to treat a map/struct as an assignment or not, but, like I said, I'm relatively new to HCL, so I'm curious what other people think here.

@jmcshane
Copy link

So, im back here in 2021 with an update. Basically all hashicorp tools have moved on to HCL2, but there isn't another great library that I've found for encoding structs to hcl text...

I've hacked the library in a couple ways that I will be the first to admit are straight up nasty. If you are interested, you can see my changes at https://github.com/jmcshane/hclencoder

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants