petergtz / pegomock

Pegomock is a powerful, yet simple mocking framework for the Go programming language
Apache License 2.0
252 stars 28 forks source link

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

Open jpopadak opened 4 years ago

jpopadak commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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!