josharian / impl

impl generates method stubs for implementing an interface.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generate parameter names

nyarly opened this issue · comments

When an interface definition leaves its method parameters anonymous, it would be nice if impl would generate a name for the variable.

This is granted non-trivial, since there are scoping problems - named results, the receiver, and other parameters might all collide.

Or at least error with a message that allows the user to understand why it crashed. Currently it looks like this:

panic: 13:30: expected operand, found ',' (and 1 more errors)

goroutine 1 [running]:
main.genStubs(0x7ffeefbff5f1, 0x16, 0xc420189200, 0x3, 0x4, 0x0, 0x0, 0x11b50c0)
	/Users/zwass/dev/go/src/github.com/groob/mockimpl/impl.go:294 +0x340
main.main()
	/Users/zwass/dev/go/src/github.com/groob/mockimpl/impl.go:373 +0x19e
datastore.go:12: running "mockimpl": exit status 2

One reasonable fix here might be to use _ for all anonymous method params. This will compile out of the gate and won't generate collisions.

A PR implementing this would be welcome.

Glanced at this. I think a minor template update would do it. E.g. in

({{range .Params}}{{.Name}} {{.Type}}, {{end}})"

we just need to write _ for .Name if .Name has len 0.

optimistically cc @danilvpetrov just in case you're interesting in also fixing this (much simpler and easier than the fix you have been working on)

Yeah no probs, will open up PR soon.