diff --git a/catalog/resource_metastore_test.go b/catalog/resource_metastore_test.go index d162cd6b2d..5ca1c46445 100644 --- a/catalog/resource_metastore_test.go +++ b/catalog/resource_metastore_test.go @@ -124,7 +124,6 @@ func TestCreateMetastore_DeltaSharing(t *testing.T) { DeltaSharingScope: "INTERNAL_AND_EXTERNAL", DeltaSharingRecipientTokenLifetimeInSeconds: 0, DeltaSharingOrganizationName: "acme", - ForceSendFields: []string{"DeltaSharingRecipientTokenLifetimeInSeconds"}, }, }, }, @@ -402,7 +401,6 @@ func TestCreateAccountMetastore_DeltaSharing(t *testing.T) { DeltaSharingScope: "INTERNAL_AND_EXTERNAL", DeltaSharingRecipientTokenLifetimeInSeconds: 0, DeltaSharingOrganizationName: "acme", - ForceSendFields: []string{"DeltaSharingRecipientTokenLifetimeInSeconds"}, }, }, }, diff --git a/common/reflect_cache.go b/common/reflect_cache.go deleted file mode 100644 index a1fb4a6d7c..0000000000 --- a/common/reflect_cache.go +++ /dev/null @@ -1,34 +0,0 @@ -package common - -import ( - "reflect" - "sync" -) - -var mutex sync.Mutex - -var mapCache = map[reflect.Type]map[string]string{} - -func getJsonToFieldNameMap(structType reflect.Type) map[string]string { - if structType.Kind() != reflect.Ptr && structType.Kind() != reflect.Struct { - return nil - } - mutex.Lock() - res, ok := mapCache[structType] - mutex.Unlock() - if ok { - return res - } - res = map[string]string{} - for i := 0; i < structType.NumField(); i++ { - field := structType.Field(i) - fieldName := chooseFieldName(field) - if fieldName != "-" { - res[fieldName] = field.Name - } - } - mutex.Lock() - mapCache[structType] = res - mutex.Unlock() - return res -} diff --git a/common/reflect_resource.go b/common/reflect_resource.go index a70a0a4946..66dbd81774 100644 --- a/common/reflect_resource.go +++ b/common/reflect_resource.go @@ -1,7 +1,6 @@ package common import ( - "errors" "fmt" "log" "reflect" @@ -39,18 +38,6 @@ var kindMap = map[reflect.Kind]string{ reflect.UnsafePointer: "UnsafePointer", } -var basicTypes = map[schema.ValueType]bool{ - schema.TypeInt: true, - schema.TypeString: true, - schema.TypeBool: true, - schema.TypeFloat: true, -} - -func isBasicType(v schema.ValueType) bool { - b, ok := basicTypes[v] - return b && ok -} - func reflectKind(k reflect.Kind) string { n, ok := kindMap[k] if !ok { @@ -500,7 +487,6 @@ func StructToData(result any, s map[string]*schema.Schema, d *schema.ResourceDat // TF SDK - feel free to replace the usages of this interface in a PR. type attributeGetter interface { GetOk(key string) (any, bool) - GetOkExists(key string) (any, bool) } // DiffToStructPointer reads resource diff with given schema onto result pointer. Panics. @@ -538,17 +524,9 @@ func DataToReflectValue(d *schema.ResourceData, r *schema.Resource, rv reflect.V func readReflectValueFromData(path []string, d attributeGetter, rv reflect.Value, s map[string]*schema.Schema) error { - names := getJsonToFieldNameMap(rv.Type()) - forceSendFields := []string{} - err := iterFields(rv, path, s, func(fieldSchema *schema.Schema, + return iterFields(rv, path, s, func(fieldSchema *schema.Schema, path []string, valueField *reflect.Value) error { fieldPath := strings.Join(path, ".") - alias := path[len(path)-1] - // GetOk returns false for default fields. - // Using GetOkExists determine which fields should be added to ForceSendFields. - if _, exists := d.GetOkExists(fieldPath); exists && isBasicType(fieldSchema.Type) { - forceSendFields = append(forceSendFields, names[alias]) - } raw, ok := d.GetOk(fieldPath) if !ok { return nil @@ -597,35 +575,6 @@ func readReflectValueFromData(path []string, d attributeGetter, } return nil }) - if err != nil { - return err - } - return setForceSendFields(rv, forceSendFields) -} - -func setForceSendFields(rv reflect.Value, presentFields []string) error { - if len(presentFields) == 0 { - return nil - } - - if rv.Kind() != reflect.Ptr && rv.Kind() != reflect.Struct { - return nil - } - - field := rv.FieldByName("ForceSendFields") - - if !field.IsValid() { - return nil - } - - if !field.CanSet() || field.Kind() != reflect.Slice { - return errors.New("cannot set field") - } - - presentFieldsValue := reflect.ValueOf(presentFields) - field.Set(presentFieldsValue) - - return nil } func primitiveReflectValueFromInterface(rk reflect.Kind, diff --git a/common/reflect_resource_test.go b/common/reflect_resource_test.go index 529f0cb4e3..a6cfdf9b6d 100644 --- a/common/reflect_resource_test.go +++ b/common/reflect_resource_test.go @@ -467,10 +467,6 @@ func (a data) GetOk(key string) (any, bool) { return v, ok } -func (a data) GetOkExists(key string) (any, bool) { - return a.GetOk(key) -} - func TestDiffToStructPointerPanic(t *testing.T) { type Nonsense struct { New int `json:"new,omitempty"` @@ -581,69 +577,11 @@ func TestStructToData_CornerCases(t *testing.T) { assert.NoError(t, err) } -type forceSendFieldsStruct struct { - Int int `json:"int"` - ForceSendFields []string -} - -var resource = func() *schema.Resource { - return DataResource(forceSendFieldsStruct{}, func(ctx context.Context, e any, c *DatabricksClient) error { - return nil - }) -}() - func TestDataToReflectValueBypass(t *testing.T) { err := DataToReflectValue(nil, &schema.Resource{Schema: map[string]*schema.Schema{}}, reflect.ValueOf(0)) assert.EqualError(t, err, "value of Struct is expected, but got Int: 0") } -func TestDeserializeForceSendFields(t *testing.T) { - - jobSchema := StructToSchema(forceSendFieldsStruct{}, - func(s map[string]*schema.Schema) map[string]*schema.Schema { - return s - }) - - d := resource.TestResourceData() - d.Set("int", 3) - var result forceSendFieldsStruct - DataToStructPointer(d, jobSchema, &result) - - assert.Equal(t, result.Int, 3) - assert.Equal(t, result.ForceSendFields, []string{"Int"}) -} - -func TestDeserializeForceSendFieldsZero(t *testing.T) { - - jobSchema := StructToSchema(forceSendFieldsStruct{}, - func(s map[string]*schema.Schema) map[string]*schema.Schema { - return s - }) - - d := resource.TestResourceData() - d.Set("int", 0) - var result forceSendFieldsStruct - DataToStructPointer(d, jobSchema, &result) - - assert.Equal(t, result.Int, 0) - assert.Equal(t, result.ForceSendFields, []string{"Int"}) -} - -func TestDeserializeForceSendFieldsNotSpecified(t *testing.T) { - - jobSchema := StructToSchema(forceSendFieldsStruct{}, - func(s map[string]*schema.Schema) map[string]*schema.Schema { - return s - }) - - d := resource.TestResourceData() - var result forceSendFieldsStruct - DataToStructPointer(d, jobSchema, &result) - - assert.Equal(t, result.Int, 0) - assert.Equal(t, result.ForceSendFields, []string(nil)) -} - func TestDataResource(t *testing.T) { r := func() *schema.Resource { type entry struct {