Skip to content

Commit 8c4b98f

Browse files
denikIsaac
andcommitted
direct: reuse fillNonZero in copier test, tidy table and README
Drop the duplicate reflection filler in favor of the existing fillNonZero, populating ForceSendFields with a small dedicated helper. Field-per-line table in TestSafeConvert; shorten the README section. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent b0d45c0 commit 8c4b98f

2 files changed

Lines changed: 55 additions & 55 deletions

File tree

‎bundle/direct/dresources/README.md‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,15 @@ Declaring a field under `hashed_fields` in `resources.yml` makes the engine pers
7575

7676
## RemapState is a dumb copy; DoRead owns all remapping
7777

78-
Most resources do not need a `RemapState` method at all. When `StateType` is a plain subset
79-
of `RemoteType`, the framework auto-generates the copy from the two types: `buildCopiers`
80-
(in `all.go`) compiles a copier per resource at package init, matching fields by JSON name,
81-
filtering `ForceSendFields`, and applying only lossless conversions. Anything it cannot copy
82-
safely (a kind-changing conversion, a mismatched struct shape) fails at load, so a resource
83-
that needs real logic surfaces as a build error rather than silent drift. Write a `RemapState`
84-
method only for that logic (derived fields, renames, per-field `ForceSendFields` rules); the
85-
rest of this section governs those hand-written overrides.
86-
87-
`RemapState` converts `RemoteType` to `StateType` only because `StateType` is typically a
88-
subset of `RemoteType`. It must be a field-by-field copy (or no-op), never a place for
78+
New resources should not define `RemapState`. When `StateType` is a subset of `RemoteType`,
79+
the framework copies it automatically: `buildCopiers` (in `all.go`) compiles a copier per
80+
resource at package init, matching fields by JSON name, filtering `ForceSendFields`, and
81+
applying only lossless conversions. Anything it cannot copy safely (a kind-changing conversion,
82+
a mismatched struct shape) fails at load rather than as silent drift. Add a `RemapState` method
83+
only for logic the copier cannot express — derived fields, renames, per-field `ForceSendFields`
84+
rules — and the rest of this section governs those overrides.
85+
86+
Such a `RemapState` must still be a field-by-field copy (or no-op), never a place for arbitrary
8987
logic. In particular, do not remap a differently-named field there (e.g. `state.x = remote.status.x`).
9088
Any remapping the API requires belongs in `DoRead`: add `x` directly to `RemoteType` and
9189
populate it from `status.x` inside `DoRead`.

‎bundle/direct/dresources/copier_test.go‎

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func TestNoRedundantRemapState(t *testing.T) {
4141
}
4242

4343
remote := reflect.New(remoteType.Elem())
44-
fillValue(remote.Elem(), 0, map[reflect.Type]bool{})
44+
fillNonZero(remote.Elem(), 0)
45+
setForceSendFields(remote.Elem())
4546

4647
want := m.Call([]reflect.Value{remote})[0].Interface()
4748
got := copier.copy(remote.Interface())
@@ -111,11 +112,36 @@ func TestSafeConvert(t *testing.T) {
111112
src reflect.Type
112113
want bool
113114
}{
114-
{"identical", reflect.TypeFor[string](), reflect.TypeFor[string](), true},
115-
{"same underlying string enums", reflect.TypeFor[copierKindA](), reflect.TypeFor[copierKindB](), true},
116-
{"int to string", reflect.TypeFor[string](), reflect.TypeFor[int](), false},
117-
{"int64 to int32", reflect.TypeFor[int32](), reflect.TypeFor[int64](), false},
118-
{"bytes to string", reflect.TypeFor[string](), reflect.TypeFor[[]byte](), false},
115+
{
116+
name: "identical",
117+
dst: reflect.TypeFor[string](),
118+
src: reflect.TypeFor[string](),
119+
want: true,
120+
},
121+
{
122+
name: "same underlying string enums",
123+
dst: reflect.TypeFor[copierKindA](),
124+
src: reflect.TypeFor[copierKindB](),
125+
want: true,
126+
},
127+
{
128+
name: "int to string",
129+
dst: reflect.TypeFor[string](),
130+
src: reflect.TypeFor[int](),
131+
want: false,
132+
},
133+
{
134+
name: "int64 to int32",
135+
dst: reflect.TypeFor[int32](),
136+
src: reflect.TypeFor[int64](),
137+
want: false,
138+
},
139+
{
140+
name: "bytes to string",
141+
dst: reflect.TypeFor[string](),
142+
src: reflect.TypeFor[[]byte](),
143+
want: false,
144+
},
119145
}
120146
for _, tt := range tests {
121147
t.Run(tt.name, func(t *testing.T) {
@@ -124,58 +150,34 @@ func TestSafeConvert(t *testing.T) {
124150
}
125151
}
126152

127-
// fillValue sets every exported field to a non-zero value, bounded by depth to avoid cycles.
128-
// Each struct's ForceSendFields is populated with its own field names so the FSF-filtering
129-
// path is exercised at every level.
130-
func fillValue(v reflect.Value, depth int, visited map[reflect.Type]bool) {
131-
if depth > 5 {
132-
return
133-
}
153+
// setForceSendFields recursively sets every struct's ForceSendFields to its own field names,
154+
// so the copier's ForceSendFields-filtering path is exercised at every level. fillNonZero
155+
// deliberately leaves ForceSendFields empty (round-trip tests need that), so we populate it
156+
// here. Map values are not addressable and cannot hold a relevant FSF source, so they are skipped.
157+
func setForceSendFields(v reflect.Value) {
134158
switch v.Kind() {
135-
case reflect.String:
136-
v.SetString("x")
137-
case reflect.Bool:
138-
v.SetBool(true)
139-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
140-
v.SetInt(1)
141-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
142-
v.SetUint(1)
143-
case reflect.Float32, reflect.Float64:
144-
v.SetFloat(1)
145159
case reflect.Pointer:
146-
v.Set(reflect.New(v.Type().Elem()))
147-
fillValue(v.Elem(), depth+1, visited)
160+
if !v.IsNil() {
161+
setForceSendFields(v.Elem())
162+
}
148163
case reflect.Slice:
149-
s := reflect.MakeSlice(v.Type(), 1, 1)
150-
fillValue(s.Index(0), depth+1, visited)
151-
v.Set(s)
152-
case reflect.Map:
153-
m := reflect.MakeMap(v.Type())
154-
key := reflect.New(v.Type().Key()).Elem()
155-
fillValue(key, depth+1, visited)
156-
val := reflect.New(v.Type().Elem()).Elem()
157-
fillValue(val, depth+1, visited)
158-
m.SetMapIndex(key, val)
159-
v.Set(m)
160-
case reflect.Struct:
161-
if visited[v.Type()] {
162-
return
164+
for i := range v.Len() {
165+
setForceSendFields(v.Index(i))
163166
}
164-
visited[v.Type()] = true
165-
defer delete(visited, v.Type())
167+
case reflect.Struct:
166168
var names []string
167169
for i := range v.NumField() {
168170
sf := v.Type().Field(i)
169-
if sf.PkgPath != "" || sf.Name == "ForceSendFields" {
171+
if !sf.IsExported() || sf.Name == "ForceSendFields" {
170172
continue
171173
}
172174
names = append(names, sf.Name)
173-
fillValue(v.Field(i), depth+1, visited)
175+
setForceSendFields(v.Field(i))
174176
}
175177
if f := v.FieldByName("ForceSendFields"); f.IsValid() && f.Kind() == reflect.Slice && f.Type().Elem().Kind() == reflect.String {
176178
f.Set(reflect.ValueOf(names))
177179
}
178180
default:
179-
// other kinds (interface, chan, func, array, complex, uintptr, ...) are left zero
181+
// scalars and other kinds have no nested ForceSendFields to set
180182
}
181183
}

0 commit comments

Comments
 (0)