Skip to content

Commit

Permalink
Added support for flattening remain tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddieowens committed Apr 27, 2020
1 parent 7c9a36a commit 9a38643
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 14 additions & 2 deletions gohcl/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,20 @@ func decodeBlockToValue(block *hcl.Block, ctx *hcl.EvalContext, v reflect.Value)
if len(block.Labels) > 0 {
blockTags := getFieldTags(ty)
for li, lv := range block.Labels {
lfieldIdx := blockTags.Labels[li].FieldIndex
v.Field(lfieldIdx).Set(reflect.ValueOf(lv))
// Fill in the labels from the block itself and then fill in labels located in embedded structs
if li < len(blockTags.Labels) {
lfieldIdx := blockTags.Labels[li].FieldIndex
v.Field(lfieldIdx).Set(reflect.ValueOf(lv))
} else {
labelIdx := li - len(blockTags.Labels)
remainIdx := *blockTags.Remain
remain := v.Type().Field(remainIdx)
remainV := v.Field(remainIdx)
remainTags := getFieldTags(remain.Type)
lfieldIdx := remainTags.Labels[labelIdx].FieldIndex
value := reflect.ValueOf(lv)
remainV.Field(lfieldIdx).Set(value)
}
}
}

Expand Down
17 changes: 13 additions & 4 deletions gohcl/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,19 @@ func ImpliedBodySchema(val interface{}) (schema *hcl.BodySchema, partial bool) {
}
ftags := getFieldTags(fty)
var labelNames []string
if len(ftags.Labels) > 0 {
labelNames = make([]string, len(ftags.Labels))
for i, l := range ftags.Labels {
labelNames[i] = l.Name
for _, l := range ftags.Labels {
labelNames = append(labelNames, l.Name)
}

// If there are labels in an embedded struct, add them as well
if ftags.Remain != nil {
remainIdx := *ftags.Remain
remain := fty.Field(remainIdx)
if remain.Type.Kind() == reflect.Struct {
remainTags := getFieldTags(remain.Type)
for _, l := range remainTags.Labels {
labelNames = append(labelNames, l.Name)
}
}
}

Expand Down

0 comments on commit 9a38643

Please sign in to comment.