gohcl: Composition of types is not supported
- Dominant language
- Go
- Stars
- 5.8k
- Forks
- 657
- Avg merge
- 20h 36m
- Merged PRs (30d)
- 6
Description
### Issue description
When using composition of a Go structures, the HCL cannot be parsed using `gohcl`.
### Steps to reproduce the issue
1. Sample code file:
```go
package main
import (
"encoding/json"
"fmt"
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hclparse"
)
type File struct {
A A `hcl:"a,block" json:"a"`
}
type A struct {
B
}
type B struct {
Foo string `json:"foo" hcl:"foo"`
}
var hclBody = []byte(`
a {
foo = "bar"
}
`)
var jsonBody = []byte(`
{
"a":{
"foo":"bar"
}
}`)
func main() {
// JSON
var j File
if err := json.Unmarshal(jsonBody, &j); err != nil {
fmt.Println("json.Unmarshal:", err)
return
}
js, _ := json.MarshalIndent(j, "", "\t")
fmt.Println(string(js))
// HCL
p := hclparse.NewParser()
f, err := p.ParseHCL(hclBody, "test.hcl")
if err != nil {
fmt.Println("p.ParseHCL:", err)
return
}
var h File
if err := gohcl.DecodeBody(f.Body, nil, &h); err != nil {
fmt.Println("gohcl.DecodeBody:", err)
return
}
hs, _ := json.MarshalIndent(h, "", "\t")
fmt.Println(string(hs))
}
```
2. Run the file to see the given error
3. Move the `Foo string` from `B` into `A`
4. Run the file again to see the file working
### What's the expected result
The file is correctly parsed as if `Foo string` from `B` was in `A`, just like its the case with JSON
Output after doing step `3`:
```json
{
"a": {
"foo": "bar"
}
}
{
"a": {
"foo": "bar"
}
}
```
### What's the actual result
Error is being thrown
```json
{
"a": {
"foo": "bar"
}
}
gohcl.DecodeBody: test.hcl:3,2-5: Unsupported argument; An argument named "foo" is not expected here.
```
### Additional details
- Go Version: `go version go1.12.5 darwin/amd64`
- Ran `go get -u` just before running the command, hcl2 at version `v0.0.0-20190515223218-4b22149b7cef`
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the supplied File/A/B example through hclparse.ParseHCL and gohcl.DecodeBody. Trace how the embedded B field is handled compared with Foo declared directly on A; done means the sample decodes foo="bar" without the unsupported-argument error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100