hashicorp / hashicorp/hcl

hclwrite support for a list of traversals

Open
#347 4 comments 6 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

Hi, I'm working on writing some terraform configuration using `hclwrite` and I'm wondering how to best define an attribute's value as a list of traversals. I see examples for lists via `cty.Value` and `SetAttributeValue` but nothing for traversals. Is it possible with `SetAttributeTraversal` or do I need to resort to `SetAttributeRaw`?

Heres a simplified version of what I have:

```go
package main

import (
"fmt"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
)

func main() {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
resBlock := rootBody.AppendNewBlock("resource", []string{"aws_route53_record", "foo"})
resBody := resBlock.Body()
traversal := hcl.Traversal{
hcl.TraverseRoot{Name: "local"},
hcl.TraverseAttr{Name: "name"},
}
resBody.SetAttributeTraversal("name", traversal)
fmt.Printf("%s\n", f.Bytes())
}
```

### HCL Template
```hcl
resource "aws_route53_record" "foo" {
name = local.name
}
```

What I'm trying to achieve is something like:

```hcl
resource "aws_route53_record" "foo" {
name = local.name

records = [local.foo, local.bar]
}
```

I don't see any examples in the docs or any test cases that do this, so any example code for this would be appreciated. thanks!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the hclwrite APIs named in the issue, SetAttributeTraversal and SetAttributeRaw, and compare them with the existing cty.Value list examples. Determine how hclwrite should represent a list containing multiple traversals, with the desired output being records = [local.foo, local.bar].

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.