Skip to content

Commit e9e2d63

Browse files
denikIsaac
andauthored
structs: use only a struct's own ForceSendFields in structdiff and structwalk (#7224)
`structdiff` and `structwalk` looked up `ForceSendFields` with `FieldByName`, which also finds one promoted from an embedded struct, and used it for the outer struct's own fields. That list can only name the embedded struct's fields (the SDK marshaller rejects other names), so it was only ever applied by mistake: to an outer `omitempty` field that shadows an embedded one. Both now look only at the struct that declares the field, as `structaccess` and `libs/dyn/convert` already do. No current type is affected: the shadowing fields are `resources.ClusterPolicy` `definition` / `policy_family_definition_overrides`, whose embedded list config loading never fills with those names, and `QualityMonitor`/`QualityMonitorState` `table_name`, which is not `omitempty`. This pull request and its description were written by Isaac. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent aed6702 commit e9e2d63

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎libs/structs/structdiff/diff.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,14 @@ func getForceSendFields(v reflect.Value) []string {
356356
if !v.IsValid() || v.Kind() != reflect.Struct {
357357
return nil
358358
}
359-
fsField := v.FieldByName("ForceSendFields")
360-
if !fsField.IsValid() || fsField.Kind() != reflect.Slice {
359+
// Only the struct's own ForceSendFields: one promoted from an embedded struct names that
360+
// struct's fields, since the SDK marshaller rejects names its struct does not have.
361+
sf, ok := v.Type().FieldByName("ForceSendFields")
362+
if !ok || len(sf.Index) != 1 {
363+
return nil
364+
}
365+
fsField := v.Field(sf.Index[0])
366+
if fsField.Kind() != reflect.Slice {
361367
return nil
362368
}
363369
result, ok := reflect.TypeAssert[[]string](fsField)

‎libs/structs/structdiff/diff_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,3 +1125,31 @@ func TestGetStructDiffPathsSharingPrefix(t *testing.T) {
11251125
assert.Equal(t, Change{Path: changes[2].Path, Old: nil, New: "2"}, changes[2])
11261126
assert.Equal(t, Change{Path: changes[3].Path, Old: "1", New: nil}, changes[3])
11271127
}
1128+
1129+
type PromotedInner struct {
1130+
A string `json:"a,omitempty"`
1131+
ForceSendFields []string `json:"-"`
1132+
}
1133+
1134+
type promotedOuter struct {
1135+
PromotedInner
1136+
Own string `json:"own,omitempty"`
1137+
}
1138+
1139+
// The embedded struct's ForceSendFields does not force the outer struct's own fields;
1140+
// the SDK marshaller would reject a name its struct does not have.
1141+
func TestGetStructDiffEmbeddedForceSendFieldsDoNotApplyToOuterFields(t *testing.T) {
1142+
forced := promotedOuter{ForceSendFields: []string{"A", "Own"}}
1143+
plain := promotedOuter{}
1144+
1145+
changes, err := GetStructDiff(forced, plain, nil)
1146+
require.NoError(t, err)
1147+
assert.Equal(t, []ResolvedChange{{Field: "a", Old: "", New: nil}}, resolveChanges(changes))
1148+
assert.False(t, IsEqual(forced, plain))
1149+
1150+
forced.ForceSendFields = []string{"Own"}
1151+
changes, err = GetStructDiff(forced, plain, nil)
1152+
require.NoError(t, err)
1153+
assert.Empty(t, changes)
1154+
assert.True(t, IsEqual(forced, plain))
1155+
}

‎libs/structs/structwalk/walk.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,14 @@ func getForceSendFields(v reflect.Value) []string {
155155
if !v.IsValid() || v.Kind() != reflect.Struct {
156156
return nil
157157
}
158-
fsField := v.FieldByName("ForceSendFields")
159-
if !fsField.IsValid() || fsField.Kind() != reflect.Slice {
158+
// Only the struct's own ForceSendFields: one promoted from an embedded struct names that
159+
// struct's fields, since the SDK marshaller rejects names its struct does not have.
160+
sf, ok := v.Type().FieldByName("ForceSendFields")
161+
if !ok || len(sf.Index) != 1 {
162+
return nil
163+
}
164+
fsField := v.Field(sf.Index[0])
165+
if fsField.Kind() != reflect.Slice {
160166
return nil
161167
}
162168
result, ok := reflect.TypeAssert[[]string](fsField)

‎libs/structs/structwalk/walk_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,20 @@ func walkPaths(t *testing.T, value any) []string {
320320
slices.Sort(paths)
321321
return paths
322322
}
323+
324+
type PromotedInner struct {
325+
A string `json:"a,omitempty"`
326+
ForceSendFields []string `json:"-"`
327+
}
328+
329+
type promotedOuter struct {
330+
PromotedInner
331+
Own string `json:"own,omitempty"`
332+
}
333+
334+
// The embedded struct's ForceSendFields does not force the outer struct's own fields;
335+
// the SDK marshaller would reject a name its struct does not have.
336+
func TestEmbeddedForceSendFieldsDoNotApplyToOuterFields(t *testing.T) {
337+
v := promotedOuter{ForceSendFields: []string{"A", "Own"}}
338+
assert.Equal(t, map[string]any{"a": ""}, flatten(t, v))
339+
}

0 commit comments

Comments
 (0)