Skip to content

Commit 0d9c1da

Browse files
authored
Merge pull request #507 from jmalloc/remove-hcl-block-decode
gohcl: Fix docs about supported types for blocks.
2 parents 2926ca3 + 163105d commit 0d9c1da

2 files changed

Lines changed: 9 additions & 29 deletions

File tree

gohcl/decode.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -258,32 +258,14 @@ func decodeBodyToMap(body hcl.Body, ctx *hcl.EvalContext, v reflect.Value) hcl.D
258258
}
259259

260260
func decodeBlockToValue(block *hcl.Block, ctx *hcl.EvalContext, v reflect.Value) hcl.Diagnostics {
261-
var diags hcl.Diagnostics
262-
263-
ty := v.Type()
264-
265-
switch {
266-
case blockType.AssignableTo(ty):
267-
v.Elem().Set(reflect.ValueOf(block))
268-
case bodyType.AssignableTo(ty):
269-
v.Elem().Set(reflect.ValueOf(block.Body))
270-
case attrsType.AssignableTo(ty):
271-
attrs, attrsDiags := block.Body.JustAttributes()
272-
if len(attrsDiags) > 0 {
273-
diags = append(diags, attrsDiags...)
274-
}
275-
v.Elem().Set(reflect.ValueOf(attrs))
276-
default:
277-
diags = append(diags, decodeBodyToValue(block.Body, ctx, v)...)
261+
diags := decodeBodyToValue(block.Body, ctx, v)
278262

279-
if len(block.Labels) > 0 {
280-
blockTags := getFieldTags(ty)
281-
for li, lv := range block.Labels {
282-
lfieldIdx := blockTags.Labels[li].FieldIndex
283-
v.Field(lfieldIdx).Set(reflect.ValueOf(lv))
284-
}
263+
if len(block.Labels) > 0 {
264+
blockTags := getFieldTags(v.Type())
265+
for li, lv := range block.Labels {
266+
lfieldIdx := blockTags.Labels[li].FieldIndex
267+
v.Field(lfieldIdx).Set(reflect.ValueOf(lv))
285268
}
286-
287269
}
288270

289271
return diags

gohcl/doc.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
// expression is assigned, or of any type accepted by gocty, in which case
2525
// gocty will be used to assign the value to a native Go type.
2626
//
27-
// "block" fields may be of type *hcl.Block or hcl.Body, in which case the
28-
// corresponding raw value is assigned, or may be a struct that recursively
29-
// uses the same tags. Block fields may also be slices of any of these types,
30-
// in which case multiple blocks of the corresponding type are decoded into
31-
// the slice.
27+
// "block" fields may be a struct that recursively uses the same tags, or a
28+
// slice of such structs, in which case multiple blocks of the corresponding
29+
// type are decoded into the slice.
3230
//
3331
// "body" can be placed on a single field of type hcl.Body to capture
3432
// the full hcl.Body that was decoded for a block. This does not allow leftover

0 commit comments

Comments
 (0)