This repository was archived by the owner on Jun 27, 2023. It is now read-only.
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
SetArg on slice silently fails #27
Closed
Description
When SetArg is used to set a slice argument, it silently fails to set the argument when the call occurs. This is because setting a slice by reflection cannot be done with a one-shot Set() call.
The diff below adds in support for setting of slices. A similar method will probably be needed for setting arrays, maps, and possible other types (maybe structs?), though my project doesn't have a need for these, so I didn't investigate their implementation.
--- github.com/golang/mock/gomock/call.go 2016-03-09 12:34:55.000000000 -0600
+++ github.com/golang/mock/gomock/call.go 2016-03-09 12:34:56.000000000 -0600
@@ -39,6 +39,13 @@
@@ -212,9 +233,38 @@
}
action = func() { c.doFunc.Call(doArgs) }
}
for n, v := range c.setArgs {
- reflect.ValueOf(args[n]).Elem().Set(v)
- }
+ switch reflect.TypeOf(args[n]).Kind() {
+ case reflect.Slice:
+ setSlice(args[n], v)
+ default:
+ reflect.ValueOf(args[n]).Elem().Set(v)
+ }
+ }
rets = c.rets
if rets == nil {
@@ -229,6 +279,15 @@
return
}
+func setSlice(arg interface{}, v reflect.Value) {
+ arg = reflect.MakeSlice(v.Type(), v.Len(), v.Cap())
+ av := reflect.ValueOf(arg)
+ for i := 0; i < av.Len(); i++ {
+ av.Index(i).Set(v.Index(i))
+ }
+}
+
func (c *Call) methodType() reflect.Type {
recv := reflect.ValueOf(c.receiver)
for i := 0; i < recv.Type().NumMethod(); i++ {
Metadata
Metadata
Assignees
Labels
No labels