Skip to content

Commit

Permalink
Add ability to decode expression to cty.Type
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarfors committed Dec 31, 2020
1 parent 94940f5 commit 51a5baa
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions gohcl/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/zclconf/go-cty/cty"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/zclconf/go-cty/cty/convert"
"github.com/zclconf/go-cty/cty/gocty"
)
Expand Down Expand Up @@ -304,6 +305,27 @@ func decodeBlockToValue(block *hcl.Block, ctx *hcl.EvalContext, v reflect.Value)
// may still be accessed by a careful caller for static analysis and editor
// integration use-cases.
func DecodeExpression(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics {
// BEGIN EDIT: decode to cty.Type if that is the src
if _, ok := val.(*cty.Type); ok {
ctyType, diags := typeexpr.TypeConstraint(expr)
if diags.HasErrors() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsuitable type expr",
Detail: fmt.Sprintf("Unsuitable type expr: %s", diags.Error()),
Subject: expr.StartRange().Ptr(),
Context: expr.Range().Ptr(),
})
return diags
}

// assign the ctyType to the target field
target := reflect.ValueOf(val).Elem()
target.Set(reflect.ValueOf(ctyType))
return diags
}
// END EDIT

srcVal, diags := expr.Value(ctx)

convTy, err := gocty.ImpliedType(val)
Expand Down

0 comments on commit 51a5baa

Please sign in to comment.