Skip to content

Commit 7bea4cd

Browse files
committed
PR feedback
1 parent fee9c8d commit 7bea4cd

File tree

4 files changed

+102
-65
lines changed

4 files changed

+102
-65
lines changed

internal/elasticsearch/enrich/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ func (r *enrichPolicyResource) upsert(ctx context.Context, plan tfsdk.Plan, stat
5252
}
5353

5454
// Convert framework types to model
55-
indices := setTypeToSlice_String(ctx, data.Indices, path.Empty(), &diags)
55+
indices := utils.SetTypeAs[string](ctx, data.Indices, path.Empty(), &diags)
5656
if diags.HasError() {
5757
return diags
5858
}
5959

60-
enrichFields := setTypeToSlice_String(ctx, data.EnrichFields, path.Empty(), &diags)
60+
enrichFields := utils.SetTypeAs[string](ctx, data.EnrichFields, path.Empty(), &diags)
6161
if diags.HasError() {
6262
return diags
6363
}

internal/elasticsearch/enrich/models.go

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/elastic/terraform-provider-elasticstack/internal/models"
77
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
88
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
9-
"github.com/hashicorp/terraform-plugin-framework/attr"
109
"github.com/hashicorp/terraform-plugin-framework/diag"
1110
"github.com/hashicorp/terraform-plugin-framework/path"
1211
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -41,47 +40,10 @@ func (data *EnrichPolicyData) populateFromPolicy(ctx context.Context, policy *mo
4140
}
4241

4342
// Convert string slices to Set
44-
data.Indices = sliceToSetType_String(ctx, policy.Indices, path.Empty(), diagnostics)
43+
data.Indices = utils.SetValueFrom(ctx, policy.Indices, types.StringType, path.Empty(), diagnostics)
4544
if diagnostics.HasError() {
4645
return
4746
}
4847

49-
data.EnrichFields = sliceToSetType_String(ctx, policy.EnrichFields, path.Empty(), diagnostics)
50-
}
51-
52-
// setTypeToSlice_String converts a types.Set to []string
53-
func setTypeToSlice_String(ctx context.Context, value types.Set, p path.Path, diags *diag.Diagnostics) []string {
54-
if value.IsNull() || value.IsUnknown() {
55-
return nil
56-
}
57-
58-
var elements []types.String
59-
d := value.ElementsAs(ctx, &elements, false)
60-
diags.Append(utils.ConvertToAttrDiags(d, p)...)
61-
if diags.HasError() {
62-
return nil
63-
}
64-
65-
result := make([]string, len(elements))
66-
for i, elem := range elements {
67-
result[i] = elem.ValueString()
68-
}
69-
return result
70-
}
71-
72-
// sliceToSetType_String converts a []string to types.Set
73-
func sliceToSetType_String(ctx context.Context, value []string, p path.Path, diags *diag.Diagnostics) types.Set {
74-
if value == nil {
75-
return types.SetNull(types.StringType)
76-
}
77-
78-
// Convert []string to []attr.Value
79-
elements := make([]attr.Value, len(value))
80-
for i, v := range value {
81-
elements[i] = types.StringValue(v)
82-
}
83-
84-
set, d := types.SetValue(types.StringType, elements)
85-
diags.Append(utils.ConvertToAttrDiags(d, p)...)
86-
return set
48+
data.EnrichFields = utils.SetValueFrom(ctx, policy.EnrichFields, types.StringType, path.Empty(), diagnostics)
8749
}

internal/utils/tfsdk.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1520
type 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.
109130
func 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.
147161
func 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.
186197
func 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.
193202
func 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
// ===================

internal/utils/tfsdk_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ var (
100100
types.StringValue("v3"),
101101
})
102102

103+
awareSetUnk = types.SetUnknown(awareType)
104+
awareSetNil = types.SetNull(awareType)
105+
awareSetEmpty = types.SetValueMust(awareType, []attr.Value{})
106+
awareSetFull = types.SetValueMust(awareType, []attr.Value{
107+
types.ObjectValueMust(awareType.AttrTypes, map[string]attr.Value{"id": types.StringValue("id1")}),
108+
types.ObjectValueMust(awareType.AttrTypes, map[string]attr.Value{"id": types.StringValue("id2")}),
109+
types.ObjectValueMust(awareType.AttrTypes, map[string]attr.Value{"id": types.StringValue("id3")}),
110+
})
111+
103112
normUnk = jsontypes.NewNormalizedUnknown()
104113
normNil = jsontypes.NewNormalizedNull()
105114
normEmpty = jsontypes.NewNormalizedValue(`{}`)
@@ -699,3 +708,52 @@ func TestTransformMapToSlice(t *testing.T) {
699708
})
700709
}
701710
}
711+
712+
// Sets
713+
714+
func TestSetTypeAs(t *testing.T) {
715+
t.Parallel()
716+
717+
tests := []struct {
718+
name string
719+
input types.Set
720+
want []aware
721+
}{
722+
{name: "converts unknown", input: awareSetUnk, want: awareSliceNil},
723+
{name: "converts nil", input: awareSetNil, want: awareSliceNil},
724+
{name: "converts empty", input: awareSetEmpty, want: awareSliceEmpty},
725+
{name: "converts struct", input: awareSetFull, want: awareSliceFull},
726+
}
727+
728+
for _, tt := range tests {
729+
t.Run(tt.name, func(t *testing.T) {
730+
var diags diag.Diagnostics
731+
got := utils.SetTypeAs[aware](context.Background(), tt.input, path.Empty(), &diags)
732+
require.Equal(t, tt.want, got)
733+
require.Empty(t, diags)
734+
})
735+
}
736+
}
737+
738+
func TestSetValueFrom(t *testing.T) {
739+
t.Parallel()
740+
741+
tests := []struct {
742+
name string
743+
input []aware
744+
want types.Set
745+
}{
746+
{name: "converts nil", input: awareSliceNil, want: awareSetNil},
747+
{name: "converts empty", input: awareSliceEmpty, want: awareSetEmpty},
748+
{name: "converts struct", input: awareSliceFull, want: awareSetFull},
749+
}
750+
751+
for _, tt := range tests {
752+
t.Run(tt.name, func(t *testing.T) {
753+
var diags diag.Diagnostics
754+
got := utils.SetValueFrom(context.Background(), tt.input, awareType, path.Empty(), &diags)
755+
require.Equal(t, tt.want, got)
756+
require.Empty(t, diags)
757+
})
758+
}
759+
}

0 commit comments

Comments
 (0)