hashicorp / hashicorp/hcl

How to define a struct to parse list of objects resource in hcl file. Keep getting `panic: unsuitable DecodeExpression target: no cty.Type (no cty field tags)`

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

Description

### HCL Template
```hcl
resource "test" "test-policy" {
src_security_zone = src_sz
dst_security_zone = dst_sz
name = "testpolicy"
acl_type = "pba"
applied_on = "dst"
approval_ticket = "20501"
ports = [{ approval_ticket = "23653", port_range = "9000", protocol = "tcp" }, { approval_ticket = "23653", port_range = "10504", protocol = "tcp" },]
}
```
I defined the structure for parsing the above HCL file as below.

```
type NaclPolicyPort struct {
PortRange string `hcl:"port_range,attr"`
Protocol string `hcl:"protocol,attr"`
ApprovalTicket string `hcl:"approval_ticket,attr"`
}
type NaclPolicyResource struct{
LabelNaclPolicy string `hcl:"name,label"`
LabelNaclPolicyName string `hcl:"name,label"`

Name string `hcl:"name,attr"`
SrcSecurityZone string `hcl:"src_security_zone,attr"`
DstSecurityZone string `hcl:"dst_security_zone,attr"`
AclType string `hcl:"acl_type,attr"`
AppliedOn string `hcl:"applied_on,attr"`
ApprovalTicket string `hcl:"approval_ticket,attr"`
Ports []NaclPolicyPort `hcl:"ports"`
}
```

### Expected behavior
What should have happened?
I get an error when my program tries to parse hcl:ports as below.
```panic: unsuitable DecodeExpression target: no cty.Type for main.NaclPolicyPort (no cty field tags)```

### Actual behavior
What actually happened?
I was expecting this to be parsed and I should have been able to get the ports.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.