hashicorp / hashicorp/hcl

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

Open
#423 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

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

```golang
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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing gohcl/decode.go at DecodeExpression and the proposed dependency on ext/typeexpr. Confirm whether gohcl should depend on that package, then determine the behavior for a target field of type cty.Type; done means supported type expressions are decoded there with appropriate diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.