Interface convertion with structs returned from a plugin

Got a weird problem here. A app that i’m developing is based on plugins. In one of this plugins i return a struct that implements a interface defined at the application. The problem is, i can’t do a interface convertion with this object.

The compiler gives me this error: panic: interface conversion: *mongodb.Document is not flare.DocumentRepositorier: missing method FindOne. But if i print all the methods from the struct, it print this:

Delete
FindOne
FindOneWithRevision
Update

So, the FindOne is there, but it still don’t let me convert the struct. Anyone already got something similar?

func (c *config) documentRepository() (flare.DocumentRepositorier, error) {
	obj := c.plugin.getObj("flare.DocumentRepositorier")
	if obj == nil {
		return nil, errors.New("don't found any plugin that implements the flare.DocumentRepositorier")
	}

	repo, ok := obj.(flare.DocumentRepositorier)
	if !ok {
		return nil, errors.New("could not cast the plugin obj to flare.DocumentRepositorier")
	}
	return repo, nil
}

Inside the plugin, this code works:

var x interface{}
x = document
_ = x.(flare.DocumentRepositorier)

It’s the same thing that is happening when the application load the plugin. But, don’t know how, in the app, it don’t work.

The document variable is the obj from the previous example.

Found the error:

# github.com/diegobernardes/flare/services/flare
../config.go:44:5: cannot use x (type *mongodb.Document) as type "github.com/diegobernardes/flare".DocumentRepositorier in assignment:
	*mongodb.Document does not implement "github.com/diegobernardes/flare".DocumentRepositorier (wrong type for FindOne method)
		have FindOne(context.Context, string) (*"github.com/diegobernardes/flare-mongodb/vendor/github.com/diegobernardes/flare".Document, error)
		want FindOne(context.Context, string) (*"github.com/diegobernardes/flare".Document, error)

This is a know issue with plugins and vendor.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.