Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: Consider Adding Collection Type ValueFrom Functions #520

Closed
bflad opened this issue Oct 21, 2022 · 1 comment · Fixed by #522
Closed

types: Consider Adding Collection Type ValueFrom Functions #520

bflad opened this issue Oct 21, 2022 · 1 comment · Fixed by #522
Assignees
Labels
enhancement New feature or request tf-devex-triage Terraform DevEx project tracking
Milestone

Comments

@bflad
Copy link
Member

bflad commented Oct 21, 2022

Module version

v0.15.0 (not yet released)

Use-cases

Provider developers can find it difficult to convert populated API SDK types or standard Go types (e.g. map[string]string]) to the framework collection types (e.g. types.Map). While there are various ways to get this working, this requires some additional discovery beyond using just the types package, which is not a great experience.

Attempted Solutions

Using tfsdk.ValueFrom(), which I do not believe is documented in the website documentation:

var fwMap types.Map
diags := tfsdk.ValueFrom(
  ctx,
  map[string]string{"test-key": "test-value"},
  types.MapType{ElemType: types.StringType},
  &fwMap,
)

Proposal

As part of #502, there will be new types value type creation functions such as:

  • func ListValue(elementType attr.Type, elements []attr.Value) types.List
  • func MapValue(elementType attr.Type, elements map[string]attr.Value) types.Map
  • func SetValue(elementType attr.Type, elements []attr.Value) types.Set

The framework could also introduce these related creation functions, which use the same underlying logic as tfsdk.ValueFrom() to perform reflection from non-framework types.

  • func ListValueFrom(ctx context.Context, elementType attr.Type, elements []any) (types.List, diag.Diagnostics)
  • func MapValueFrom(ctx context.Context, elementType attr.Type, elements map[string]any) (types.Map, diag.Diagnostics)
  • func SetValueFrom(ctx context.Context, elementType attr.Type, elements []any) (types.Set, diag.Diagnostics)

Compared to tfsdk.ValueFrom(), they would automatically handle the collection type (only requiring the element type) and offer a slight amount of additional compiler checking over just any.

References

@bflad bflad added enhancement New feature or request tf-devex-triage Terraform DevEx project tracking labels Oct 21, 2022
bflad added a commit that referenced this issue Oct 24, 2022
…tValueFrom functions

Reference: #520

These will enable provider developers to use the framework type system's built-in conversion rules to create collection types, rather than using more generic `tfsdk.ValueFrom()` or other methodologies.

In this example, a map using standard Go types is used to create a `types.Map` framework type with known values:

```go
apiMap := map[string]string{
  "key1": "value1",
  "key2": "value2",
}
fwMap, diags := types.MapValueFrom(ctx, types.StringType, apiMap)
```

There may be additional use cases or needs that get teased out with this introduction, such as the ability to create a `types.Object` from a `map[string]any`, however those can be handled in potential future feature requests.
@bflad bflad added this to the v0.15.0 milestone Oct 24, 2022
@bflad bflad self-assigned this Oct 24, 2022
bflad added a commit that referenced this issue Oct 25, 2022
…tValueFrom functions (#522)

Reference: #520

These will enable provider developers to use the framework type system's built-in conversion rules to create collection types, rather than using more generic `tfsdk.ValueFrom()` or other methodologies.

In this example, a map using standard Go types is used to create a `types.Map` framework type with known values:

```go
apiMap := map[string]string{
  "key1": "value1",
  "key2": "value2",
}
fwMap, diags := types.MapValueFrom(ctx, types.StringType, apiMap)
```

There may be additional use cases or needs that get teased out with this introduction, such as the ability to create a `types.Object` from a `map[string]any`, however those can be handled in potential future feature requests.
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request tf-devex-triage Terraform DevEx project tracking
Projects
None yet
1 participant