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

go/src/plugin: proposed generate wrap code when build plugin #30882

Closed
rocket049 opened this issue Mar 16, 2019 · 11 comments
Closed

go/src/plugin: proposed generate wrap code when build plugin #30882

rocket049 opened this issue Mar 16, 2019 · 11 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@rocket049
Copy link

If we have follow plugin code:

//plugin.go
package main

import (
	"fmt"
)

type ObjType struct {
	name string
}

func (p *ObjType) Say(s string) int {
	fmt.Printf("%s say: %s\n", p.name, s)
	return len(s)
}

func NewObjType(name string) *ObjType {
	return &ObjType{name}
}

When we build it like this:
go build -buildmode=plugin plugin.go

It is better to generate wrap code like follow:

//pluginloader.go
package main

import (
	"plugin"
	"reflect"
)

type ObjTypeInterface interface {
	Say(string) int
}

type PluginLoader struct {
	p *plugin.Plugin
}

func (p *PluginLoader) NewObjType(s string) ObjTypeInterface {
	f0, err := p.p.Lookup("NewObjType")
	if err != nil {
		panic(err)
	}
	f1 := reflect.ValueOf(f0)
	p1 := reflect.ValueOf(s)
	res := f1.Call([]reflect.Value{p1})
	if res == nil {
		return nil
	}
	return res[0].Interface().(ObjTypeInterface)
}

func PluginLoad(name string) (p *PluginLoader) {
	plug, err := plugin.Open(name)
	if err != nil {
		panic(err)
	}
	return &PluginLoader{p: plug}
}
@rocket049
Copy link
Author

I do something for it.

code:

https://github.com/rocket049/pluginloader

@rocket049 rocket049 reopened this Mar 18, 2019
@katiehockman
Copy link
Contributor

@rocket049 Can you please clarify what the actual output of go build -buildmode=plugin plugin.go is, versus what you are proposing? It's unclear from the issue description what problem that the proposed solution would be trying to solve. Is there a bug that you feel your solution is fixing?

@katiehockman katiehockman added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 19, 2019
@katiehockman katiehockman changed the title Suggest generate wrap code when build plugin go/src/plugin: proposed generate wrap code when build plugin Mar 19, 2019
@rocket049
Copy link
Author

The problem is too complex to use plugin.
I want to use plugin simple,
call function and use object from a plugin natural.
Just like gomobile does!

@rocket049
Copy link
Author

Let go generate wrap code for user, when build a plugin.

@helperShang
Copy link

@rocket049 No sense to do this , i can reflect by myself.

@rocket049
Copy link
Author

Vitality lies in progress!

@rocket049
Copy link
Author

I write a wrap tool: github.com/rocket049/pluginloader/cmd/pluginwrap
It works well with github.com/rocket049/pluginloader/
I really don't like reflect ,it is slow.
But it seems no way to convert a function by type assert when it returns an object (struct with methods).

@rocket049
Copy link
Author

pluginwrap is almost perfect now!

@agnivade
Copy link
Contributor

@rocket049 - You seem to have a tool which works for you. Is there anything else you want to be done in this issue ? What you seem to ask is likely a proposal anyways.

@agnivade agnivade added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Jun 12, 2019
@rocket049
Copy link
Author

@rocket049 - You seem to have a tool which works for you. Is there anything else you want to be done in this issue ? What you seem to ask is likely a proposal anyways.

I have no proposal now. Because I have resolve it myself. Should I close this issue?

@agnivade
Copy link
Contributor

If there is nothing to do from our side, then yes. I will go ahead and close it.

@golang golang locked and limited conversation to collaborators Jun 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants