-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type composition is broken (embedded fields not detected) #583
Comments
Hi @dargueta by adding Remain hcl.Body `hcl:",remain"` to the structs, I don't get the error, true. But if I fmt.Printf("output.Stuff[0].A: %#v\n", output.Stuff[0].A)
fmt.Printf("output.Stuff[0].B: %#v\n", output.Stuff[0].B) I get
which is not wanted, I guess. |
So embedded structs like type B struct {
A
// ...
} are ignored. In if content == nil {
return diags
}
// start included
t := val.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldValue := val.Field(i)
if field.Anonymous && fieldValue.Kind() == reflect.Struct {
embeddedStructPtr := reflect.New(field.Type).Interface()
if diag := DecodeBody(body, ctx, embeddedStructPtr); diag.HasErrors() {
diags = append(diags, diag...)
continue
}
fieldValue.Set(reflect.ValueOf(embeddedStructPtr).Elem())
}
}
// end included
tags := getFieldTags(val.Type()) which for type As struct { // renamed to As instead of A to distinguish from the field A
A string `hcl:"a"`
Remain hcl.Body `hcl:",remain"`
}
type B struct {
As
B int `hcl:"b"`
Remain hcl.Body `hcl:",remain"`
}
type File struct {
Stuff []B `hcl:"stuff,block"`
}
func main() {
hclContent := `
stuff {
a = "asdf"
b = 123
}
`
parser := hclparse.NewParser()
file, diag := parser.ParseHCL([]byte(hclContent), "config.hcl")
if diag.HasErrors() {
fmt.Println("Error parsing HCL:", diag)
return
}
ctx := &hcl.EvalContext{}
var output File
if err := gohcl.DecodeBody(file.Body, ctx, &output); err.HasErrors() {
fmt.Println("Error decoding HCL:", err)
return
}
fmt.Printf("output.Stuff[0].A: %#v\n", output.Stuff[0].A)
fmt.Printf("output.Stuff[0].B: %#v\n", output.Stuff[0].B)
} happened to produce
This was just an ugly attempt to see how I can modify the code to get the result in this specific situation. But I'm not experienced enough with the hcl repo code to actually produce a PR that really works. |
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 #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:
This should load correctly
What Happens
Unfortunately, I get an error:
If I comment out
a
then it works.Reproduction Example
Environment
OS: Ubuntu 22.04.1
Go: 1.19.4
hcl: v2.15.0
The text was updated successfully, but these errors were encountered: