diff --git a/internal/providers/pluginfw/converters/converters_test.go b/internal/providers/pluginfw/converters/converters_test.go index aca2ac1874..245f4f68f0 100644 --- a/internal/providers/pluginfw/converters/converters_test.go +++ b/internal/providers/pluginfw/converters/converters_test.go @@ -7,6 +7,7 @@ import ( "testing" pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common" + tfcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" @@ -49,32 +50,6 @@ func (DummyTfSdk) GetComplexFieldTypes(ctx context.Context) map[string]reflect.T } } -func (DummyTfSdk) ToObjectType(ctx context.Context) types.ObjectType { - return types.ObjectType{ - AttrTypes: map[string]attr.Type{ - "enabled": types.BoolType, - "workers": types.Int64Type, - "floats": types.Float64Type, - "description": types.StringType, - "task": types.StringType, - "no_pointer_nested": types.ListType{ElemType: DummyNestedTfSdk{}.ToObjectType(ctx)}, - "nested_list": types.ListType{ElemType: DummyNestedTfSdk{}.ToObjectType(ctx)}, - "nested_pointer_list": types.ListType{ElemType: DummyNestedTfSdk{}.ToObjectType(ctx)}, - "map": types.MapType{ElemType: types.StringType}, - "nested_map": types.MapType{ElemType: DummyNestedTfSdk{}.ToObjectType(ctx)}, - "repeated": types.ListType{ElemType: types.Int64Type}, - "attributes": types.MapType{ElemType: types.StringType}, - "enum_field": types.StringType, - "additional_field": types.StringType, - "distinct_field": types.StringType, - "slice_struct_ptr": types.ListType{ElemType: DummyNestedTfSdk{}.ToObjectType(ctx)}, - "object": types.ObjectType{ - AttrTypes: DummyNestedTfSdk{}.ToObjectType(ctx).AttrTypes, - }, - }, - } -} - type TestEnum string const TestEnumA TestEnum = `TEST_ENUM_A` @@ -107,15 +82,6 @@ type DummyNestedTfSdk struct { Enabled types.Bool `tfsdk:"enabled" tf:"optional"` } -func (DummyNestedTfSdk) ToObjectType(ctx context.Context) types.ObjectType { - return types.ObjectType{ - AttrTypes: map[string]attr.Type{ - "name": types.StringType, - "enabled": types.BoolType, - }, - } -} - type DummyGoSdk struct { Enabled bool `json:"enabled"` Workers int64 `json:"workers"` @@ -150,24 +116,35 @@ func populateEmptyFields(c DummyTfSdk) DummyTfSdk { complexFields := c.GetComplexFieldTypes(context.Background()) v := reflect.ValueOf(&c).Elem() for i := 0; i < v.NumField(); i++ { - name := v.Type().Field(i).Name - tfsdkName := v.Type().Field(i).Tag.Get("tfsdk") - complexType, ok := complexFields[tfsdkName] - if !ok { + field := v.Field(i) + // If the field is a simple type, the zero value is OK. + switch field.Type() { + case reflect.TypeOf(types.Bool{}), reflect.TypeOf(types.Int64{}), reflect.TypeOf(types.Float64{}), reflect.TypeOf(types.String{}): continue } - field := v.FieldByName(name) if !field.IsZero() { continue } - innerVal := reflect.New(complexType).Elem().Interface() + + tfsdkName := v.Type().Field(i).Tag.Get("tfsdk") + complexType, ok := complexFields[tfsdkName] + if !ok { + continue + } + var typ attr.Type - if ot, ok := innerVal.(interface { - ToObjectType(context.Context) types.ObjectType - }); ok { - typ = ot.ToObjectType(context.Background()) - } else { - typ = innerVal.(attr.Value).Type(context.Background()) + switch complexType { + case reflect.TypeOf(types.Bool{}): + typ = types.BoolType + case reflect.TypeOf(types.Int64{}): + typ = types.Int64Type + case reflect.TypeOf(types.Float64{}): + typ = types.Float64Type + case reflect.TypeOf(types.String{}): + typ = types.StringType + default: + innerVal := reflect.New(complexType).Elem().Interface() + typ = tfcommon.NewObjectValuable(innerVal).Type(context.Background()) } switch field.Type() { case reflect.TypeOf(types.List{}): @@ -221,7 +198,7 @@ func TestGoSdkToTfSdkStructConversionFailure(t *testing.T) { assert.True(t, actualDiagnostics.Equal(expectedDiagnostics)) } -var dummyType = DummyNestedTfSdk{}.ToObjectType(context.Background()) +var dummyType = tfcommon.NewObjectValuable(DummyNestedTfSdk{}).Type(context.Background()).(types.ObjectType) var tests = []struct { name string