Skip to content

Commit bc05cff

Browse files
authored
Revert "Handle ForceSendFields (#2753)" (#2843)
This reverts commit cff5d80.
1 parent b60be7b commit bc05cff

File tree

4 files changed

+1
-150
lines changed

4 files changed

+1
-150
lines changed

catalog/resource_metastore_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ func TestCreateMetastore_DeltaSharing(t *testing.T) {
124124
DeltaSharingScope: "INTERNAL_AND_EXTERNAL",
125125
DeltaSharingRecipientTokenLifetimeInSeconds: 0,
126126
DeltaSharingOrganizationName: "acme",
127-
ForceSendFields: []string{"DeltaSharingRecipientTokenLifetimeInSeconds"},
128127
},
129128
},
130129
},
@@ -402,7 +401,6 @@ func TestCreateAccountMetastore_DeltaSharing(t *testing.T) {
402401
DeltaSharingScope: "INTERNAL_AND_EXTERNAL",
403402
DeltaSharingRecipientTokenLifetimeInSeconds: 0,
404403
DeltaSharingOrganizationName: "acme",
405-
ForceSendFields: []string{"DeltaSharingRecipientTokenLifetimeInSeconds"},
406404
},
407405
},
408406
},

common/reflect_cache.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

common/reflect_resource.go

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package common
22

33
import (
4-
"errors"
54
"fmt"
65
"log"
76
"reflect"
@@ -39,18 +38,6 @@ var kindMap = map[reflect.Kind]string{
3938
reflect.UnsafePointer: "UnsafePointer",
4039
}
4140

42-
var basicTypes = map[schema.ValueType]bool{
43-
schema.TypeInt: true,
44-
schema.TypeString: true,
45-
schema.TypeBool: true,
46-
schema.TypeFloat: true,
47-
}
48-
49-
func isBasicType(v schema.ValueType) bool {
50-
b, ok := basicTypes[v]
51-
return b && ok
52-
}
53-
5441
func reflectKind(k reflect.Kind) string {
5542
n, ok := kindMap[k]
5643
if !ok {
@@ -500,7 +487,6 @@ func StructToData(result any, s map[string]*schema.Schema, d *schema.ResourceDat
500487
// TF SDK - feel free to replace the usages of this interface in a PR.
501488
type attributeGetter interface {
502489
GetOk(key string) (any, bool)
503-
GetOkExists(key string) (any, bool)
504490
}
505491

506492
// 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
538524

539525
func readReflectValueFromData(path []string, d attributeGetter,
540526
rv reflect.Value, s map[string]*schema.Schema) error {
541-
names := getJsonToFieldNameMap(rv.Type())
542-
forceSendFields := []string{}
543-
err := iterFields(rv, path, s, func(fieldSchema *schema.Schema,
527+
return iterFields(rv, path, s, func(fieldSchema *schema.Schema,
544528
path []string, valueField *reflect.Value) error {
545529
fieldPath := strings.Join(path, ".")
546-
alias := path[len(path)-1]
547-
// GetOk returns false for default fields.
548-
// Using GetOkExists determine which fields should be added to ForceSendFields.
549-
if _, exists := d.GetOkExists(fieldPath); exists && isBasicType(fieldSchema.Type) {
550-
forceSendFields = append(forceSendFields, names[alias])
551-
}
552530
raw, ok := d.GetOk(fieldPath)
553531
if !ok {
554532
return nil
@@ -597,35 +575,6 @@ func readReflectValueFromData(path []string, d attributeGetter,
597575
}
598576
return nil
599577
})
600-
if err != nil {
601-
return err
602-
}
603-
return setForceSendFields(rv, forceSendFields)
604-
}
605-
606-
func setForceSendFields(rv reflect.Value, presentFields []string) error {
607-
if len(presentFields) == 0 {
608-
return nil
609-
}
610-
611-
if rv.Kind() != reflect.Ptr && rv.Kind() != reflect.Struct {
612-
return nil
613-
}
614-
615-
field := rv.FieldByName("ForceSendFields")
616-
617-
if !field.IsValid() {
618-
return nil
619-
}
620-
621-
if !field.CanSet() || field.Kind() != reflect.Slice {
622-
return errors.New("cannot set field")
623-
}
624-
625-
presentFieldsValue := reflect.ValueOf(presentFields)
626-
field.Set(presentFieldsValue)
627-
628-
return nil
629578
}
630579

631580
func primitiveReflectValueFromInterface(rk reflect.Kind,

common/reflect_resource_test.go

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,6 @@ func (a data) GetOk(key string) (any, bool) {
467467
return v, ok
468468
}
469469

470-
func (a data) GetOkExists(key string) (any, bool) {
471-
return a.GetOk(key)
472-
}
473-
474470
func TestDiffToStructPointerPanic(t *testing.T) {
475471
type Nonsense struct {
476472
New int `json:"new,omitempty"`
@@ -581,69 +577,11 @@ func TestStructToData_CornerCases(t *testing.T) {
581577
assert.NoError(t, err)
582578
}
583579

584-
type forceSendFieldsStruct struct {
585-
Int int `json:"int"`
586-
ForceSendFields []string
587-
}
588-
589-
var resource = func() *schema.Resource {
590-
return DataResource(forceSendFieldsStruct{}, func(ctx context.Context, e any, c *DatabricksClient) error {
591-
return nil
592-
})
593-
}()
594-
595580
func TestDataToReflectValueBypass(t *testing.T) {
596581
err := DataToReflectValue(nil, &schema.Resource{Schema: map[string]*schema.Schema{}}, reflect.ValueOf(0))
597582
assert.EqualError(t, err, "value of Struct is expected, but got Int: 0")
598583
}
599584

600-
func TestDeserializeForceSendFields(t *testing.T) {
601-
602-
jobSchema := StructToSchema(forceSendFieldsStruct{},
603-
func(s map[string]*schema.Schema) map[string]*schema.Schema {
604-
return s
605-
})
606-
607-
d := resource.TestResourceData()
608-
d.Set("int", 3)
609-
var result forceSendFieldsStruct
610-
DataToStructPointer(d, jobSchema, &result)
611-
612-
assert.Equal(t, result.Int, 3)
613-
assert.Equal(t, result.ForceSendFields, []string{"Int"})
614-
}
615-
616-
func TestDeserializeForceSendFieldsZero(t *testing.T) {
617-
618-
jobSchema := StructToSchema(forceSendFieldsStruct{},
619-
func(s map[string]*schema.Schema) map[string]*schema.Schema {
620-
return s
621-
})
622-
623-
d := resource.TestResourceData()
624-
d.Set("int", 0)
625-
var result forceSendFieldsStruct
626-
DataToStructPointer(d, jobSchema, &result)
627-
628-
assert.Equal(t, result.Int, 0)
629-
assert.Equal(t, result.ForceSendFields, []string{"Int"})
630-
}
631-
632-
func TestDeserializeForceSendFieldsNotSpecified(t *testing.T) {
633-
634-
jobSchema := StructToSchema(forceSendFieldsStruct{},
635-
func(s map[string]*schema.Schema) map[string]*schema.Schema {
636-
return s
637-
})
638-
639-
d := resource.TestResourceData()
640-
var result forceSendFieldsStruct
641-
DataToStructPointer(d, jobSchema, &result)
642-
643-
assert.Equal(t, result.Int, 0)
644-
assert.Equal(t, result.ForceSendFields, []string(nil))
645-
}
646-
647585
func TestDataResource(t *testing.T) {
648586
r := func() *schema.Resource {
649587
type entry struct {

0 commit comments

Comments
 (0)