hcl icon indicating copy to clipboard operation
hcl copied to clipboard

Consider `gohcl` supporting `typeexpr` when target value is `cty.Type`

Open jlarfors opened this issue 4 years ago • 0 comments

Are you willing to accept a dependency on the ext/typeexpr package from gohcl?

If so, we would gladly provide a PR to support typeexpr when the target field in a golang struct is cty.Type to the DecodeExpression method in hcl2/gohcl/decode.go

https://github.com/hashicorp/hcl/blob/3bb0644ad8b106c43c9868fd6501b5a5456213b4/gohcl/decode.go#L306

func (s *Scope) DecodeExpression(ctx *decodeContext, expr hcl.Expression, val interface{}) hcl.Diagnostics {
	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
	}
        // ... rest of the function ...
}

But I understand if this dependency on ext packages should be avoided in the generic gohcl package. Currently we have a copy of some of the files from gohcl to support what we want.

jlarfors avatar Nov 23 '20 13:11 jlarfors