Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to implement TODO implement optionalPackageOf for *model.FuncType #100

Open
jpopadak opened this issue Feb 11, 2020 · 2 comments

Comments

@jpopadak
Copy link
Contributor

jpopadak commented Feb 11, 2020

We are receiving this TODO while trying to generate some mocks.

Funny thing is, it works for a vararg version of this function, but not a slice version of it.

It works for this:

type Processor interface {
	Validate(path string, claims map[string]interface{},
		validators ...func(map[string]interface{}) error) error
}

But does not work for this:

type Processor interface {
	Validate(path string, claims map[string]interface{},
		validators []func(map[string]interface{}) error,
	) error
}

Question:
What does the logic do in github.com/petergtz/pegomock/mockgen/mockgen.go function optionalPackageOf? I would like to submit a PR to implement this.

@jpopadak
Copy link
Contributor Author

So, in mockgen.go, I added the following to the switch statement causing the panic it successfully creates the Mock itself.

Added code to the switch statement:

	case *model.FuncType:
		var types = make(map[model.Type]struct{})
		for _, inParam := range typedType.In {
			types[inParam.Type]= struct{}{}
		}
		for _, outParam := range typedType.Out {
			types[outParam.Type]= struct{}{}
		}
		if typedType.Variadic != nil {
			types[typedType.Variadic.Type]= struct{}{}
		}
		packages := strings.Builder{}
		for t := range types {
			if packages.Len() > 0 {
				packages.WriteRune('\n')
			}
			packages.WriteString(optionalPackageOf(t, packageMap))
		}
		return packages.String()

But when generating the matchers it creates a file named:

slice_of_todo_implement_matcher_for:_&{[0xc0000b1a00_0xc0000b1a20]_[0xc0000b1a80]_<nil>} is_type_of_*model.functype .go

This closely matches the panic message from the current version of Pegomock:

panic: TODO implement optionalPackageOf for: &{[0xc0000eb760] [0xc0000eb7e0] }

@petergtz
Copy link
Owner

Hi @jpopadak and sorry for the missing functionality :-(.

You're on the right track and your implementation above looks right. Check the file for more TODOs and you'll find more functions with switches where model.FuncType is not implemented yet. Once, all these are implemented, the errors should go away.

Thanks a lot for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants