Skip to content

Commit

Permalink
Replace 'interface{}' with 'any'
Browse files Browse the repository at this point in the history
By running the command:
gofmt -w -r 'interface{} -> any' .
  • Loading branch information
alexandear committed Jul 26, 2023
1 parent de438f1 commit 4c37ad1
Show file tree
Hide file tree
Showing 47 changed files with 335 additions and 335 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ modifies how the `Got` value is formatted:
```go
gomock.GotFormatterAdapter(
gomock.GotFormatterFunc(func(i interface{}) string {
gomock.GotFormatterFunc(func(i any) string {
// Leading 0s
return fmt.Sprintf("%02d", i)
}),
Expand Down
44 changes: 22 additions & 22 deletions gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
type Call struct {
t TestHelper // for triggering test failures on invalid call setup

receiver interface{} // the receiver of the method call
receiver any // the receiver of the method call
method string // the name of the method
methodType reflect.Type // the type of the method
args []Matcher // the args
Expand All @@ -41,12 +41,12 @@ type Call struct {
// actions are called when this Call is called. Each action gets the args and
// can set the return values by returning a non-nil slice. Actions run in the
// order they are created.
actions []func([]interface{}) []interface{}
actions []func([]any) []any
}

// newCall creates a *Call. It requires the method type in order to support
// unexported methods.
func newCall(t TestHelper, receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call {
func newCall(t TestHelper, receiver any, method string, methodType reflect.Type, args ...any) *Call {
t.Helper()

// TODO: check arity, types.
Expand All @@ -67,9 +67,9 @@ func newCall(t TestHelper, receiver interface{}, method string, methodType refle
// and this line changes, i.e. this code is wrapped in another anonymous function.
// 0 is us, 1 is RecordCallWithMethodType(), 2 is the generated recorder, and 3 is the user's test.
origin := callerInfo(3)
actions := []func([]interface{}) []interface{}{func([]interface{}) []interface{} {
actions := []func([]any) []any{func([]any) []any {
// Synthesize the zero value for each of the return args' types.
rets := make([]interface{}, methodType.NumOut())
rets := make([]any, methodType.NumOut())
for i := 0; i < methodType.NumOut(); i++ {
rets[i] = reflect.Zero(methodType.Out(i)).Interface()
}
Expand Down Expand Up @@ -107,13 +107,13 @@ func (c *Call) MaxTimes(n int) *Call {

// DoAndReturn declares the action to run when the call is matched.
// The return values from this function are returned by the mocked function.
// It takes an interface{} argument to support n-arity functions.
// It takes an any argument to support n-arity functions.
// The anonymous function must match the function signature mocked method.
func (c *Call) DoAndReturn(f interface{}) *Call {
func (c *Call) DoAndReturn(f any) *Call {
// TODO: Check arity and types here, rather than dying badly elsewhere.
v := reflect.ValueOf(f)

c.addAction(func(args []interface{}) []interface{} {
c.addAction(func(args []any) []any {
c.t.Helper()
ft := v.Type()
if c.methodType.NumIn() != ft.NumIn() {
Expand All @@ -136,7 +136,7 @@ func (c *Call) DoAndReturn(f interface{}) *Call {
}
}
vRets := v.Call(vArgs)
rets := make([]interface{}, len(vRets))
rets := make([]any, len(vRets))
for i, ret := range vRets {
rets[i] = ret.Interface()
}
Expand All @@ -148,13 +148,13 @@ func (c *Call) DoAndReturn(f interface{}) *Call {
// Do declares the action to run when the call is matched. The function's
// return values are ignored to retain backward compatibility. To use the
// return values call DoAndReturn.
// It takes an interface{} argument to support n-arity functions.
// It takes an any argument to support n-arity functions.
// The anonymous function must match the function signature mocked method.
func (c *Call) Do(f interface{}) *Call {
func (c *Call) Do(f any) *Call {
// TODO: Check arity and types here, rather than dying badly elsewhere.
v := reflect.ValueOf(f)

c.addAction(func(args []interface{}) []interface{} {
c.addAction(func(args []any) []any {
c.t.Helper()
ft := v.Type()
if c.methodType.NumIn() != ft.NumIn() {
Expand Down Expand Up @@ -183,7 +183,7 @@ func (c *Call) Do(f interface{}) *Call {
}

// Return declares the values to be returned by the mocked function call.
func (c *Call) Return(rets ...interface{}) *Call {
func (c *Call) Return(rets ...any) *Call {
c.t.Helper()

mt := c.methodType
Expand Down Expand Up @@ -215,7 +215,7 @@ func (c *Call) Return(rets ...interface{}) *Call {
}
}

c.addAction(func([]interface{}) []interface{} {
c.addAction(func([]any) []any {
return rets
})

Expand All @@ -231,7 +231,7 @@ func (c *Call) Times(n int) *Call {
// SetArg declares an action that will set the nth argument's value,
// indirected through a pointer. Or, in the case of a slice and map, SetArg
// will copy value's elements/key-value pairs into the nth argument.
func (c *Call) SetArg(n int, value interface{}) *Call {
func (c *Call) SetArg(n int, value any) *Call {
c.t.Helper()

mt := c.methodType
Expand Down Expand Up @@ -262,7 +262,7 @@ func (c *Call) SetArg(n int, value interface{}) *Call {
n, at, c.origin)
}

c.addAction(func(args []interface{}) []interface{} {
c.addAction(func(args []any) []any {
v := reflect.ValueOf(value)
switch reflect.TypeOf(args[n]).Kind() {
case reflect.Slice:
Expand Down Expand Up @@ -323,7 +323,7 @@ func (c *Call) String() string {

// Tests if the given call matches the expected call.
// If yes, returns nil. If no, returns error with message explaining why it does not match.
func (c *Call) matches(args []interface{}) error {
func (c *Call) matches(args []any) error {
if !c.methodType.IsVariadic() {
if len(args) != len(c.args) {
return fmt.Errorf("expected call at %s has the wrong number of arguments. Got: %d, want: %d",
Expand Down Expand Up @@ -429,7 +429,7 @@ func (c *Call) dropPrereqs() (preReqs []*Call) {
return
}

func (c *Call) call() []func([]interface{}) []interface{} {
func (c *Call) call() []func([]any) []any {
c.numCalls++
return c.actions
}
Expand All @@ -441,14 +441,14 @@ func InOrder(calls ...*Call) {
}
}

func setSlice(arg interface{}, v reflect.Value) {
func setSlice(arg any, v reflect.Value) {
va := reflect.ValueOf(arg)
for i := 0; i < v.Len(); i++ {
va.Index(i).Set(v.Index(i))
}
}

func setMap(arg interface{}, v reflect.Value) {
func setMap(arg any, v reflect.Value) {
va := reflect.ValueOf(arg)
for _, e := range va.MapKeys() {
va.SetMapIndex(e, reflect.Value{})
Expand All @@ -458,11 +458,11 @@ func setMap(arg interface{}, v reflect.Value) {
}
}

func (c *Call) addAction(action func([]interface{}) []interface{}) {
func (c *Call) addAction(action func([]any) []any) {
c.actions = append(c.actions, action)
}

func formatGottenArg(m Matcher, arg interface{}) string {
func formatGottenArg(m Matcher, arg any) string {
got := fmt.Sprintf("%v (%T)", arg, arg)
if gs, ok := m.(GotFormatter); ok {
got = gs.Got(arg)
Expand Down
Loading

0 comments on commit 4c37ad1

Please sign in to comment.