Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Dec 6, 2024
1 parent 5779ece commit b3634bd
Showing 1 changed file with 25 additions and 48 deletions.
73 changes: 25 additions & 48 deletions internal/providers/pluginfw/converters/converters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"

Check failure on line 9 in internal/providers/pluginfw/converters/converters_test.go

View workflow job for this annotation

GitHub Actions / tests

package "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common" is being imported more than once (ST1019)
tfcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"

Check failure on line 10 in internal/providers/pluginfw/converters/converters_test.go

View workflow job for this annotation

GitHub Actions / tests

other import of "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"
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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{}):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b3634bd

Please sign in to comment.