-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
50 lines (46 loc) · 1.13 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package common
import (
"fmt"
"os"
"plugin"
"reflect"
"github.com/oswaldoooo/cmicro/kits"
)
// name is function's name which in your plugin go filem, target is tell plugin loador your function how to made
// here is an example
// var testone func(string)string
// var test_one PluginOption={"TestOne",testone}
type PluginOption struct {
Name string
Target any //store function
}
// load plugin,return the real load count
func LoadPlugin(src *plugin.Plugin, opts ...*PluginOption) (count int) { //return successful find number
if src == nil || len(opts) == 0 {
return
}
var (
ottp reflect.Type
sy plugin.Symbol
err error
)
for _, opt := range opts {
ottp = reflect.TypeOf(opt.Target)
if len(opt.Name) > 0 && ottp.Kind() == reflect.Func {
//filter options,name must be set,and target must be func
sy, err = src.Lookup(opt.Name)
if err == nil {
if kits.Tofunc(sy, &opt.Target) {
count++
} else {
fmt.Fprintln(os.Stderr, "[tofunc failed]")
}
} else {
fmt.Fprintln(os.Stderr, "[look up error]", err.Error())
}
} else {
fmt.Fprintln(os.Stderr, "option is invaild")
}
}
return
}