You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
…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.
…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.
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.
Module version
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 thetypes
package, which is not a great experience.Attempted Solutions
Using
tfsdk.ValueFrom()
, which I do not believe is documented in the website documentation: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 justany
.References
ValueFrom
as the opposite ofValueAs
#323ValueFrom
that takes a Go value and populates a compatibleattr.Value
, given a descriptiveattr.Type
#350The text was updated successfully, but these errors were encountered: