Skip to content

Commit

Permalink
fix: issue 707
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovak committed Oct 9, 2023
1 parent f54eea9 commit 4fd9909
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
7 changes: 3 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ tasks:
- "**/*.go"
cmds:
- go fmt ./...

mocks.remove:
desc: remove all mock files
cmds:
- find . -name '*_mock.go' | xargs rm
- rm -rf mocks/
- find . -name '*_mock.go' | xargs rm
- rm -rf mocks/

mocks.generate:
desc: generate mockery mocks
Expand Down Expand Up @@ -75,6 +75,5 @@ tasks:
- task: test
- task: test.e2e


default:
deps: [test.ci]
75 changes: 75 additions & 0 deletions mocks/github.com/vektra/mockery/v2/pkg/fixtures/Variadic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/fixtures/variadic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test

type VariadicFunction = func(args1 string, args2 ...interface{}) interface{}

type Variadic interface {
VariadicFunction(str string, vFunc VariadicFunction) error
}
21 changes: 15 additions & 6 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,19 +481,19 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
case 0:
return fmt.Sprintf(
"func(%s)",
g.renderTypeTuple(ctx, t.Params()),
g.renderTypeTuple(ctx, t.Params(), t.Variadic()),
)
case 1:
return fmt.Sprintf(
"func(%s) %s",
g.renderTypeTuple(ctx, t.Params()),
g.renderTypeTuple(ctx, t.Params(), t.Variadic()),
g.renderType(ctx, t.Results().At(0).Type()),
)
default:
return fmt.Sprintf(
"func(%s)(%s)",
g.renderTypeTuple(ctx, t.Params()),
g.renderTypeTuple(ctx, t.Results()),
g.renderTypeTuple(ctx, t.Params(), t.Variadic()),
g.renderTypeTuple(ctx, t.Results(), t.Variadic()),
)
}
case *types.Map:
Expand Down Expand Up @@ -562,13 +562,22 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
}
}

func (g *Generator) renderTypeTuple(ctx context.Context, tup *types.Tuple) string {
func (g *Generator) renderTypeTuple(ctx context.Context, tup *types.Tuple, variadic bool) string {
var parts []string

for i := 0; i < tup.Len(); i++ {
v := tup.At(i)

parts = append(parts, g.renderType(ctx, v.Type()))
if variadic {
switch t := v.Type().(type) {
case *types.Slice:
parts = append(parts, "..."+g.renderType(ctx, t.Elem()))
default:
parts = append(parts, g.renderType(ctx, v.Type()))
}
} else {
parts = append(parts, g.renderType(ctx, v.Type()))
}
}

return strings.Join(parts, " , ")
Expand Down

0 comments on commit 4fd9909

Please sign in to comment.