Skip to content

Commit 501874e

Browse files
authored
internal/framework/flex: support set of enum expansion (#36789)
This change enables expansion of sets of strin enum types with AutoFlex. ```console % go test ./internal/framework/flex/... -run=TestExpandSetOfStringEnum --- FAIL: TestExpandSetOfStringEnum (0.00s) --- FAIL: TestExpandSetOfStringEnum/empty_value (0.00s) panic: reflect.Set: value of type []string is not assignable to type []flex.testEnum [recovered] panic: reflect.Set: value of type []string is not assignable to type []flex.testEnum goroutine 6 [running]: testing.tRunner.func1.2({0x1014e13a0, 0x140006ba040}) /opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1631 +0x1c4 testing.tRunner.func1() /opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1634 +0x33c panic({0x1014e13a0?, 0x140006ba040?}) /opt/homebrew/Cellar/go/1.22.2/libexec/src/runtime/panic.go:770 +0x124 reflect.Value.assignTo({0x1014dca40?, 0x140006c20a8?, 0x18?}, {0x100ffa971, 0xb}, 0x1014daf40, 0x0) /opt/homebrew/Cellar/go/1.22.2/libexec/src/reflect/value.go:3356 +0x20c reflect.Value.Set({0x1014daf40?, 0x1400000ee58?, 0x0?}, {0x1014dca40?, 0x140006c20a8?, 0x2?}) /opt/homebrew/Cellar/go/1.22.2/libexec/src/reflect/value.go:2325 +0xcc github.com/hashicorp/terraform-provider-aws/internal/framework/flex.autoExpander.setOfString({}, {0x101587de0, 0x101a3f100}, {{0x101a3f100, 0x0, 0x0}, {0x101589b78, 0x101a3f100}, 0x 2}, {0x1014daf40?, ...}) /Users/jaredbaker/development/terraform-provider-aws/internal/framework/flex/auto_expand.go:550 +0x4fc github.com/hashicorp/terraform-provider-aws/internal/framework/flex.autoExpander.set({}, {0x101587de0, 0x101a3f100}, {0x10158be50, 0x14000498b40}, {0x1014daf40?, 0x1400000ee58?, 0x8 ef00?}) /Users/jaredbaker/development/terraform-provider-aws/internal/framework/flex/auto_expand.go:507 +0x344 github.com/hashicorp/terraform-provider-aws/internal/framework/flex.autoExpander.convert({}, {0x101587de0, 0x101a3f100}, {0x101566840?, 0x14000498b40?, 0x140006b6d08?}, {0x1014daf40 ?, 0x1400000ee58?, 0x140006b6e10?}) /Users/jaredbaker/development/terraform-provider-aws/internal/framework/flex/auto_expand.go:93 +0x49c github.com/hashicorp/terraform-provider-aws/internal/framework/flex.autoFlexConvert({0x101587de0, 0x101a3f100}, {0x101566840, 0x14000498b40}, {0x1014daf80, 0x1400000ee58}, {0x101583 f88, 0x101a3f100}) /Users/jaredbaker/development/terraform-provider-aws/internal/framework/flex/autoflex.go:54 +0x214 github.com/hashicorp/terraform-provider-aws/internal/framework/flex.Expand({0x101587de0, 0x101a3f100}, {0x101566840, 0x14000498b40}, {0x1014daf80, 0x1400000ee58}, {0x0, 0x0, 0x14000 05df18?}) /Users/jaredbaker/development/terraform-provider-aws/internal/framework/flex/auto_expand.go:35 +0xb0 github.com/hashicorp/terraform-provider-aws/internal/framework/flex.runAutoExpandTestCases.func1(0x140000a96c0) /Users/jaredbaker/development/terraform-provider-aws/internal/framework/flex/auto_expand_test.go:898 +0x8c testing.tRunner(0x140000a96c0, 0x140004b52d0) /opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1689 +0xec created by testing.(*T).Run in goroutine 4 /opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1742 +0x318 FAIL github.com/hashicorp/terraform-provider-aws/internal/framework/flex 0.303s FAIL ``` After: ```console % go test ./internal/framework/flex/... -run=TestExpandSetOfStringEnum ok github.com/hashicorp/terraform-provider-aws/internal/framework/flex 0.480s ```
1 parent 3961bd6 commit 501874e

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

internal/framework/flex/auto_expand.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,13 @@ func (expander autoExpander) setOfString(ctx context.Context, vFrom basetypes.Se
539539
return diags
540540
}
541541

542-
vTo.Set(reflect.ValueOf(to))
542+
// Copy elements individually to enable expansion of lists of
543+
// custom string types (AWS enums)
544+
vals := reflect.MakeSlice(vTo.Type(), len(to), len(to))
545+
for i := 0; i < len(to); i++ {
546+
vals.Index(i).SetString(to[i])
547+
}
548+
vTo.Set(vals)
543549
return diags
544550

545551
case reflect.Ptr:

internal/framework/flex/auto_expand_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,80 @@ func TestExpandStringEnum(t *testing.T) {
731731
runAutoExpandTestCases(ctx, t, testCases)
732732
}
733733

734+
func TestExpandListOfStringEnum(t *testing.T) {
735+
t.Parallel()
736+
737+
type testEnum string
738+
var testEnumFoo testEnum = "foo"
739+
var testEnumBar testEnum = "bar"
740+
741+
var testEnums []testEnum
742+
testEnumsWant := []testEnum{testEnumFoo, testEnumBar}
743+
744+
ctx := context.Background()
745+
testCases := autoFlexTestCases{
746+
{
747+
TestName: "valid value",
748+
Source: types.ListValueMust(types.StringType, []attr.Value{
749+
types.StringValue(string(testEnumFoo)),
750+
types.StringValue(string(testEnumBar)),
751+
}),
752+
Target: &testEnums,
753+
WantTarget: &testEnumsWant,
754+
},
755+
{
756+
TestName: "empty value",
757+
Source: types.ListValueMust(types.StringType, []attr.Value{}),
758+
Target: &testEnums,
759+
WantTarget: &testEnums,
760+
},
761+
{
762+
TestName: "null value",
763+
Source: types.ListNull(types.StringType),
764+
Target: &testEnums,
765+
WantTarget: &testEnums,
766+
},
767+
}
768+
runAutoExpandTestCases(ctx, t, testCases)
769+
}
770+
771+
func TestExpandSetOfStringEnum(t *testing.T) {
772+
t.Parallel()
773+
774+
type testEnum string
775+
var testEnumFoo testEnum = "foo"
776+
var testEnumBar testEnum = "bar"
777+
778+
var testEnums []testEnum
779+
testEnumsWant := []testEnum{testEnumFoo, testEnumBar}
780+
781+
ctx := context.Background()
782+
testCases := autoFlexTestCases{
783+
{
784+
TestName: "valid value",
785+
Source: types.SetValueMust(types.StringType, []attr.Value{
786+
types.StringValue(string(testEnumFoo)),
787+
types.StringValue(string(testEnumBar)),
788+
}),
789+
Target: &testEnums,
790+
WantTarget: &testEnumsWant,
791+
},
792+
{
793+
TestName: "empty value",
794+
Source: types.SetValueMust(types.StringType, []attr.Value{}),
795+
Target: &testEnums,
796+
WantTarget: &testEnums,
797+
},
798+
{
799+
TestName: "null value",
800+
Source: types.SetNull(types.StringType),
801+
Target: &testEnums,
802+
WantTarget: &testEnums,
803+
},
804+
}
805+
runAutoExpandTestCases(ctx, t, testCases)
806+
}
807+
734808
func TestExpandSimpleNestedBlockWithStringEnum(t *testing.T) {
735809
t.Parallel()
736810

0 commit comments

Comments
 (0)