-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin_api.go
111 lines (91 loc) · 4.15 KB
/
plugin_api.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
// Autogenerated by github.com/wendigo/go-bind-plugin on 2016-12-06 08:06:59.815134005 +0100 CET, do not edit!
// Command: go-bind-plugin -plugin-path plugin.so -plugin-package ./plugin -output-name PluginAPI -output-path plugin_api.go -output-package main -dereference-vars -rebuild
//
// Plugin plugin.so info:
// - package: github.com/wendigo/go-bind-plugin-example/plugin
// - size: 2578348 bytes
// - sha256: 303cd891bd37c209c0ad0798673bd871af8e6bae3673cc7c261abb54e76e6ae9
import (
"fmt"
"plugin"
"reflect"
"strings"
)
// PluginAPI wraps symbols (functions and variables) exported by plugin github.com/wendigo/go-bind-plugin-example/plugin
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin-example/plugin
type PluginAPI struct {
// Exported functions
_CalculateSin func(float64) float64
_SayHello func(string)
// Exported variables (public references)
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin-example/plugin#CurrentYear
CurrentYear int
}
// CalculateSin function was exported from plugin github.com/wendigo/go-bind-plugin-example/plugin symbol 'CalculateSin'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin-example/plugin#CalculateSin
func (p *PluginAPI) CalculateSin(in0 float64) float64 {
return p._CalculateSin(in0)
}
// SayHello function was exported from plugin github.com/wendigo/go-bind-plugin-example/plugin symbol 'SayHello'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin-example/plugin#SayHello
func (p *PluginAPI) SayHello(in0 string) {
p._SayHello(in0)
}
// String returnes textual representation of the wrapper. It provides info on exported symbols and variables.
func (p *PluginAPI) String() string {
var lines []string
lines = append(lines, "Struct PluginAPI:")
lines = append(lines, "\t- Generated on: 2016-12-06 08:06:59.815134005 +0100 CET")
lines = append(lines, "\t- Command: go-bind-plugin -plugin-path plugin.so -plugin-package ./plugin -output-name PluginAPI -output-path plugin_api.go -output-package main -dereference-vars -rebuild")
lines = append(lines, "\nPlugin info:")
lines = append(lines, "\t- package: github.com/wendigo/go-bind-plugin-example/plugin")
lines = append(lines, "\t- sha256 sum: 303cd891bd37c209c0ad0798673bd871af8e6bae3673cc7c261abb54e76e6ae9")
lines = append(lines, "\t- size: 2578348 bytes")
lines = append(lines, "\nExported functions (2):")
lines = append(lines, "\t- CalculateSin func(float64) (float64)")
lines = append(lines, "\t- SayHello func(string)")
lines = append(lines, "\nExported variables (1):")
lines = append(lines, "\t- CurrentYear int")
return strings.Join(lines, "\n")
}
// BindPluginAPI loads plugin from the given path and binds symbols (variables and functions)
// to the PluginAPI struct. All variables are derefenenced.
func BindPluginAPI(path string) (*PluginAPI, error) {
p, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("could not open plugin: %s", err)
}
ret := new(PluginAPI)
funcCalculateSin, err := p.Lookup("CalculateSin")
if err != nil {
return nil, fmt.Errorf("could not import function 'CalculateSin', symbol not found: %s", err)
}
if typed, ok := funcCalculateSin.(func(float64) float64); ok {
ret._CalculateSin = typed
} else {
return nil, fmt.Errorf("could not import function 'CalculateSin', incompatible types 'func(float64) (float64)' and '%s'", reflect.TypeOf(funcCalculateSin))
}
funcSayHello, err := p.Lookup("SayHello")
if err != nil {
return nil, fmt.Errorf("could not import function 'SayHello', symbol not found: %s", err)
}
if typed, ok := funcSayHello.(func(string)); ok {
ret._SayHello = typed
} else {
return nil, fmt.Errorf("could not import function 'SayHello', incompatible types 'func(string)' and '%s'", reflect.TypeOf(funcSayHello))
}
varCurrentYear, err := p.Lookup("CurrentYear")
if err != nil {
return nil, fmt.Errorf("could not import variable 'CurrentYear', symbol not found: %s", err)
}
if typed, ok := varCurrentYear.(*int); ok {
ret.CurrentYear = *typed
} else {
return nil, fmt.Errorf("could not import variable 'CurrentYear', incompatible types 'int' and '%s'", reflect.TypeOf(varCurrentYear))
}
return ret, nil
}