Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfeng-db committed Jul 23, 2024
1 parent 76b0745 commit 8be0879
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
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

0 comments on commit 8be0879

Please sign in to comment.