hashicorp / hashicorp/hcl

Type composition is broken (embedded fields not detected)

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

Description

It appears that embedded fields aren't being recognized by the parser, even if they have the appropriate `hcl` tag. This is a problem because that means I can't factor out common fields into a separate struct to avoid a bunch of copying and pasting when I only need a single optional label.

This is similar to https://github.com/hashicorp/hcl/issues/136, however the major difference here is that the embedded fields _are_ tagged. It does work if you add "remain", however that precludes you from mixing two structures into a third.

### Expectation

Given the following definition:

```go
type A struct {
A string `hcl:"a"`
}

type B struct {
A
B int `hcl:"b"`
}

type File struct {
Stuff []B `hcl:"stuff,block"`
}
```

This should load correctly

```hcl
stuff {
a = "asdf"
b = 123
}
```

### What Happens

Unfortunately, I get an error:
```
Error: test.hcl:3,3-4: Unsupported argument; An argument named "a" is not expected here.
```
If I comment out `a` then it works.

### Reproduction Example

```go
package main

import (
"fmt"

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

type A struct {
A string `hcl:"a"`
}

type B struct {
A
B int `hcl:"b"`
}

type File struct {
Stuff []B `hcl:"stuff,block"`
}

func main() {
input := `
stuff {
a = "asdf"
b = 123
}
`

output := File{}
err := hclsimple.Decode("test.hcl", []byte(input), nil, &output)
fmt.Printf("Error: %v\n", err)
}
```

### Environment

OS: Ubuntu 22.04.1
Go: 1.19.4
hcl: v2.15.0

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with hclsimple.Decode and the struct-tag handling used by the embedded-field reproduction. Add a regression test covering tagged embedded fields inside a block and verify that the sample HCL loads without reporting the embedded field as unsupported.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.