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

Distinguishing attributes vs blocks #18

@jamesmharvey

Description

@jamesmharvey

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions