Skip to content

Commit

Permalink
refactor(flex): make string value to framework generic
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-b committed Dec 8, 2022
1 parent 18af067 commit 698be2e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
12 changes: 6 additions & 6 deletions internal/flex/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,19 @@ func BoolToFrameworkLegacy(_ context.Context, v *bool) types.Bool {
return types.BoolValue(aws.ToBool(v))
}

// EnumStringToFramework converts and enum string to a Framework String value.
// StringValueToFramework converts a string value to a Framework String value.
// An empty string is converted to a null String.
func EnumStringToFramework(_ context.Context, v string) types.String {
func StringValueToFramework[T ~string](_ context.Context, v T) types.String {
if v == "" {
return types.StringNull()
}
return types.StringValue(v)
return types.StringValue(string(v))
}

// EnumStringToFrameworkLegacy converts and enum string to a Framework String value.
// StringValueToFrameworkLegacy converts a string value to a Framework String value.
// An empty string is left as an empty String.
func EnumStringToFrameworkLegacy(_ context.Context, v string) types.String {
return types.StringValue(v)
func StringValueToFrameworkLegacy[T ~string](_ context.Context, v T) types.String {
return types.StringValue(string(v))
}

// Int64ToFramework converts an int64 pointer to a Framework Int64 value.
Expand Down
26 changes: 16 additions & 10 deletions internal/flex/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,19 +622,22 @@ func TestStringToFrameworkWithTransform(t *testing.T) {
}
}

func TestEnumStringToFramework(t *testing.T) {
func TestStringValueToFramework(t *testing.T) {
t.Parallel()

// AWS enums use custom types with an underlying string type
type custom string

type testCase struct {
input string
input custom
expected types.String
}
tests := map[string]testCase{
"valid enum": {
"valid": {
input: "TEST",
expected: types.StringValue("TEST"),
},
"empty enum": {
"empty": {
input: "",
expected: types.StringNull(),
},
Expand All @@ -643,7 +646,7 @@ func TestEnumStringToFramework(t *testing.T) {
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
got := EnumStringToFramework(context.Background(), test.input)
got := StringValueToFramework(context.Background(), test.input)

if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
Expand All @@ -652,19 +655,22 @@ func TestEnumStringToFramework(t *testing.T) {
}
}

func TestEnumStringToFrameworkLegacy(t *testing.T) {
func TestStringValueToFrameworkLegacy(t *testing.T) {
t.Parallel()

// AWS enums use custom types with an underlying string type
type custom string

type testCase struct {
input string
input custom
expected types.String
}
tests := map[string]testCase{
"valid enum": {
"valid": {
input: "TEST",
expected: types.StringValue("TEST"),
},
"empty enum": {
"empty": {
input: "",
expected: types.StringValue(""),
},
Expand All @@ -673,7 +679,7 @@ func TestEnumStringToFrameworkLegacy(t *testing.T) {
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
got := EnumStringToFrameworkLegacy(context.Background(), test.input)
got := StringValueToFrameworkLegacy(context.Background(), test.input)

if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
Expand Down
4 changes: 2 additions & 2 deletions internal/service/auditmanager/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func flattenControlMappingSources(ctx context.Context, apiObject []awstypes.Cont

obj := map[string]attr.Value{
"source_description": flex.StringToFramework(ctx, source.SourceDescription),
"source_frequency": flex.EnumStringToFramework(ctx, string(source.SourceFrequency)),
"source_frequency": flex.StringValueToFramework(ctx, source.SourceFrequency),
"source_id": types.StringValue(aws.ToString(source.SourceId)),
"source_keyword": sk,
"source_name": types.StringValue(aws.ToString(source.SourceName)),
Expand Down Expand Up @@ -574,7 +574,7 @@ func flattenSourceKeyword(ctx context.Context, apiObject *awstypes.SourceKeyword
}

obj := map[string]attr.Value{
"keyword_input_type": flex.EnumStringToFramework(ctx, string(apiObject.KeywordInputType)),
"keyword_input_type": flex.StringValueToFramework(ctx, apiObject.KeywordInputType),
"keyword_value": types.StringValue(aws.ToString(apiObject.KeywordValue)),
}
objVal, d := types.ObjectValue(sourceKeywordAttrTypes, obj)
Expand Down

0 comments on commit 698be2e

Please sign in to comment.