@@ -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