@@ -12,6 +12,11 @@ import (
1212 "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1313)
1414
15+ type Elementable interface {
16+ attr.Value
17+ ElementsAs (ctx context.Context , target interface {}, allowUnhandled bool ) diag.Diagnostics
18+ }
19+
1520type ListMeta struct {
1621 Context context.Context
1722 Index int
@@ -46,6 +51,22 @@ func ValueStringPointer(value types.String) *string {
4651 return value .ValueStringPointer ()
4752}
4853
54+ // ================
55+ // ==== Generic ===
56+ // ================
57+
58+ // MapTypeAs converts a types.Map into a tfsdk aware map[string]T.
59+ func elementsAs [T any ](ctx context.Context , value Elementable , p path.Path , diags * diag.Diagnostics ) T {
60+ var result T
61+ if ! IsKnown (value ) {
62+ return result
63+ }
64+
65+ d := value .ElementsAs (ctx , & result , false )
66+ diags .Append (ConvertToAttrDiags (d , p )... )
67+ return result
68+ }
69+
4970// ================
5071// ===== Maps =====
5172// ================
@@ -107,14 +128,7 @@ func MapTypeToMap[T1 any, T2 any](ctx context.Context, value types.Map, p path.P
107128
108129// MapTypeAs converts a types.Map into a tfsdk aware map[string]T.
109130func MapTypeAs [T any ](ctx context.Context , value types.Map , p path.Path , diags * diag.Diagnostics ) map [string ]T {
110- if ! IsKnown (value ) {
111- return nil
112- }
113-
114- var items map [string ]T
115- d := value .ElementsAs (ctx , & items , false )
116- diags .Append (ConvertToAttrDiags (d , p )... )
117- return items
131+ return elementsAs [map [string ]T ](ctx , value , p , diags )
118132}
119133
120134// MapValueFrom converts a tfsdk aware map[string]T to a types.Map.
@@ -145,10 +159,7 @@ func SliceToListType[T1 any, T2 any](ctx context.Context, value []T1, elemType a
145159// SliceToListType_String converts a tfsdk naive []string into a types.List.
146160// This is a shorthand SliceToListType helper for strings.
147161func SliceToListType_String (ctx context.Context , value []string , p path.Path , diags * diag.Diagnostics ) types.List {
148- return SliceToListType (ctx , value , types .StringType , p , diags ,
149- func (item string , meta ListMeta ) types.String {
150- return types .StringValue (item )
151- })
162+ return ListValueFrom (ctx , value , types .StringType , p , diags )
152163}
153164
154165// ListTypeToMap converts a types.List first into a tfsdk aware map[string]T1
@@ -184,21 +195,12 @@ func ListTypeToSlice[T1 any, T2 any](ctx context.Context, value types.List, p pa
184195// ListTypeToSlice_String converts a types.List into a []string.
185196// This is a shorthand ListTypeToSlice helper for strings.
186197func ListTypeToSlice_String (ctx context.Context , value types.List , p path.Path , diags * diag.Diagnostics ) []string {
187- return ListTypeToSlice (ctx , value , p , diags , func (item types.String , meta ListMeta ) string {
188- return item .ValueString ()
189- })
198+ return ListTypeAs [string ](ctx , value , p , diags )
190199}
191200
192201// ListTypeAs converts a types.List into a tfsdk aware []T.
193202func ListTypeAs [T any ](ctx context.Context , value types.List , p path.Path , diags * diag.Diagnostics ) []T {
194- if ! IsKnown (value ) {
195- return nil
196- }
197-
198- var items []T
199- nd := value .ElementsAs (ctx , & items , false )
200- diags .Append (ConvertToAttrDiags (nd , p )... )
201- return items
203+ return elementsAs [[]T ](ctx , value , p , diags )
202204}
203205
204206// ListValueFrom converts a tfsdk aware []T to a types.List.
@@ -208,6 +210,21 @@ func ListValueFrom[T any](ctx context.Context, value []T, elemType attr.Type, p
208210 return list
209211}
210212
213+ // ===================
214+ // ===== Sets =====
215+ // ===================
216+
217+ // SetTypeAs converts a types.Set into a tfsdk aware []T.
218+ func SetTypeAs [T any ](ctx context.Context , value types.Set , p path.Path , diags * diag.Diagnostics ) []T {
219+ return elementsAs [[]T ](ctx , value , p , diags )
220+ }
221+
222+ func SetValueFrom [T any ](ctx context.Context , value []T , elemType attr.Type , p path.Path , diags * diag.Diagnostics ) types.Set {
223+ list , d := types .SetValueFrom (ctx , elemType , value )
224+ diags .Append (ConvertToAttrDiags (d , p )... )
225+ return list
226+ }
227+
211228// ===================
212229// ===== Objects =====
213230// ===================
0 commit comments