moq icon indicating copy to clipboard operation
moq copied to clipboard

Error if field type and method have the same name

Open vadimalekseev opened this issue 3 years ago • 3 comments

Hey!

When I do go generate ./... I get an error.

package main

//go:generate moq -out mocks/some_iface.go . SomeIface

type SomeIface interface {
	Begin()
	BeginFunc()
}

Error: Type '*SomeIfaceMock' has both field and method named 'BeginFunc'

vadimalekseev avatar Mar 24 '22 21:03 vadimalekseev

Moq creates ___Func versions of the fields, so this clashes. As a workaround, you could name the field differently?

matryer avatar Mar 31 '22 10:03 matryer

As a workaround, you could name the field differently?

I want to moq the pgx.Pool, https://github.com/jackc/pgx/blob/v4.15.0/pgxpool/conn.go#L83

I cannot name the field differently, except to make my own wrapper. Can we add some checks when generating the code?

vadimalekseev avatar Apr 06 '22 10:04 vadimalekseev

I think @aleksvdim has a valid point here. I see to following options to resolve this:

  1. Provide a flag in moq, that allows to alter the suffix used to create the field names (e.g. moq -suffix MoqFunc would generate ____MoqFunc
  2. Change the suffix to something, that less likely generates a clash, e.g. MoqFunc (not backwards compatible, so I guess this is not an option.
  3. Check, if the generated fields have a conflict with one of the methods of the interface. If this is the case, use a different suffix.

breml avatar Apr 09 '22 13:04 breml