Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Make plugin framework infra functions work for Enum types #3813

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions common/reflect_resource_plugin_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func tfSdkToGoSdkSingleField(srcField reflect.Value, destField reflect.Value, sr
destMap.SetMapIndex(destMapKey, destMapValue)
}
destField.Set(destMap)
} else if srcField.Kind() == reflect.String {
// This case is only for the Enum types.
destField.SetString(srcField.String())
} else {
panic(fmt.Errorf("unknown type for field: %s", srcField.Type().Name()))
}
Expand Down Expand Up @@ -227,12 +230,17 @@ func goSdkToTfSdkSingleField(srcField reflect.Value, destField reflect.Value, sr
destField.Set(reflect.ValueOf(types.Float64Null()))
}
case reflect.String:
strVal := srcFieldValue.(string)
// check if alwaysAdd is false or the value is zero or if the field is in the forceSendFields list
if alwaysAdd || !(strVal == "" && !checkTheStringInForceSendFields(srcFieldName, forceSendField)) {
destField.Set(reflect.ValueOf(types.StringValue(strVal)))
if srcField.Type().Name() != "string" {
// This case is for Enum Types.
destField.Set(reflect.ValueOf(srcFieldValue))
} else {
destField.Set(reflect.ValueOf(types.StringNull()))
strVal := srcFieldValue.(string)
// check if alwaysAdd is false or the value is zero or if the field is in the forceSendFields list
if alwaysAdd || !(strVal == "" && !checkTheStringInForceSendFields(srcFieldName, forceSendField)) {
destField.Set(reflect.ValueOf(types.StringValue(strVal)))
} else {
destField.Set(reflect.ValueOf(types.StringNull()))
}
}
case reflect.Struct:
// resolve the nested struct by recursively calling the function
Expand Down Expand Up @@ -379,6 +387,9 @@ func pluginFrameworkTypeToSchema(v reflect.Value) map[string]schema.Attribute {
nestedScm := pluginFrameworkTypeToSchema(sv)
scm[fieldName] = schema.SingleNestedAttribute{Attributes: nestedScm, Optional: isOptional, Required: !isOptional}
}
} else if kind == reflect.String {
// This case is for Enum types.
scm[fieldName] = schema.StringAttribute{Optional: isOptional, Required: !isOptional}
} else {
panic(fmt.Errorf("unknown type for field: %s", typeField.Name))
}
Expand Down
11 changes: 11 additions & 0 deletions common/reflect_resource_plugin_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ type DummyTfSdk struct {
NestedMap map[string]DummyNestedTfSdk `tfsdk:"nested_map" tf:"optional"`
Repeated []types.Int64 `tfsdk:"repeated" tf:"optional"`
Attributes map[string]types.String `tfsdk:"attributes" tf:"optional"`
EnumField TestEnum `tfsdk:"enum_field" tf:"optional"`
Irrelevant types.String `tfsdk:"-"`
}

type TestEnum string

const TestEnumA TestEnum = `TEST_ENUM_A`

const TestEnumB TestEnum = `TEST_ENUM_B`

const TestEnumC TestEnum = `TEST_ENUM_C`

type DummyNestedTfSdk struct {
Name types.String `tfsdk:"name" tf:"optional"`
Enabled types.Bool `tfsdk:"enabled" tf:"optional"`
Expand All @@ -46,6 +55,7 @@ type DummyGoSdk struct {
NestedMap map[string]DummyNestedGoSdk `json:"nested_map"`
Repeated []int64 `json:"repeated"`
Attributes map[string]string `json:"attributes"`
EnumField TestEnum `json:"enum_field"`
ForceSendFields []string `json:"-"`
}

Expand Down Expand Up @@ -124,6 +134,7 @@ var tfSdkStruct = DummyTfSdk{
},
},
Attributes: map[string]types.String{"key": types.StringValue("value")},
EnumField: TestEnumA,
Repeated: []types.Int64{types.Int64Value(12), types.Int64Value(34)},
}

Expand Down
Loading