rjeczalik / interfaces

Code generation tools for Go.
MIT License
420 stars 31 forks source link

Interfaces syntactically incorrect for functions with functions as arguments #32

Open OscarVanL opened 2 years ago

OscarVanL commented 2 years ago

I have my own PubSub package, which has a method SubscribeWithCallback. One of the arguments to this function is a function (used as a callback in this case)...

func (ps PubSub) SubscribeWithCallback(
    c context.Context,
    sub pubsub.Subscription,
    callback func(context.Context, pubsub.Message),
) (context.Context, error) {

When I use go:generate with interfacer on my PubSub struct, the resultant interface is not syntactically correct and will not compile...

//go:generate interfacer -for github.com/my/repo/pubsub.PubSub -as psiface.PubSub -o psiface/pubsub_iface.go

pubsub_iface.go looks like this:

// Code generated by interfacer; DO NOT EDIT

package psiface

import (
    "context"
    "github.com/my/repo/psiface"
)

// PubSub is an interface generated for "github.com/my/repo/pubsub.PubSub".
type PubSub interface {
    ....
    SubscribeWithCallback(context.Context, pubsub.Subscription, func(context.Context, cloud.google.com/go/internal/pubsub.Message)) (context.Context, error)
}

This has a syntax error as cloud.google.com/go/internal/pubsub.Message is not a valid type.

It seems that some step must be missing or bugged to handle arguments of functions passed as arguments.

rjeczalik commented 1 year ago

The code responsible for reading package names in typed defs should be improved.

Related https://github.com/rjeczalik/interfaces/issues/37