Skip to content

Commit

Permalink
Revert "Handle ForceSendFields (#2753)" (#2843)
Browse files Browse the repository at this point in the history
This reverts commit cff5d80.
  • Loading branch information
mgyucht authored Oct 26, 2023
1 parent b60be7b commit bc05cff
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 150 deletions.
2 changes: 0 additions & 2 deletions catalog/resource_metastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func TestCreateMetastore_DeltaSharing(t *testing.T) {
DeltaSharingScope: "INTERNAL_AND_EXTERNAL",
DeltaSharingRecipientTokenLifetimeInSeconds: 0,
DeltaSharingOrganizationName: "acme",
ForceSendFields: []string{"DeltaSharingRecipientTokenLifetimeInSeconds"},
},
},
},
Expand Down Expand Up @@ -402,7 +401,6 @@ func TestCreateAccountMetastore_DeltaSharing(t *testing.T) {
DeltaSharingScope: "INTERNAL_AND_EXTERNAL",
DeltaSharingRecipientTokenLifetimeInSeconds: 0,
DeltaSharingOrganizationName: "acme",
ForceSendFields: []string{"DeltaSharingRecipientTokenLifetimeInSeconds"},
},
},
},
Expand Down
34 changes: 0 additions & 34 deletions common/reflect_cache.go

This file was deleted.

53 changes: 1 addition & 52 deletions common/reflect_resource.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
"errors"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
62 changes: 0 additions & 62 deletions common/reflect_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bc05cff

Please sign in to comment.