Skip to content

Commit 3ae4225

Browse files
author
Mateusz Gajewski
committed
Verbose errors on lookup error
1 parent bdb9561 commit 3ae4225

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
**go-bind-plugin** generates neat API around symbols exported by a plugin built with `go build -buildmode=plugin` in upcoming go 1.8. [plugin.Plugin](https://tip.golang.org/pkg/plugin/#Plugin) holds information about exported symbols as map[string]interface{}. go-bind-plugins uses reflection to find out actual types of symbols and generates typed API for a provided plugin with additional functionalities (like dereferencing exported variables and checking sha256 sum). Basic usage does not require plugin sources as wrapper can be generated using only `.so` file.
1111

12-
For example if plugin exports `AddTwoInts(a, b int) int` function instead of using [Plugin.Lookup](https://tip.golang.org/pkg/plugin/#Plugin.Lookup) directly:
12+
For example if plugin exports `AddTwoInts(a, b int) int` function and `BuildVersion string` variable instead of using [Plugin.Lookup](https://tip.golang.org/pkg/plugin/#Plugin.Lookup) directly:
1313

1414
```
1515
plug, err := plugin.Open("plugin.so")
@@ -25,6 +25,19 @@ if err != nil {
2525
2626
if typed, ok := symbol.(func(int, int) int); ok {
2727
result := typed(10, 20)
28+
} else {
29+
panic("AddTwoInts has different type than exported by plugin")
30+
}
31+
32+
symbol, err := plug.Lookup("BuildVersion")
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
if typed, ok := symbol.(*string); ok {
38+
fmt.Println(*typed)
39+
} else {
40+
panic("BuildVersion is not a string reference")
2841
}
2942
```
3043

@@ -38,6 +51,7 @@ if err != nil {
3851
}
3952
4053
result := plug.AddTwoInts(10, 20)
54+
fmt.Println(plug.BuildVersion) // or fmt.Println(*plug.BuildVersion) if -dereference-vars is not used
4155
```
4256

4357
`BingPluginAPI()` will ensure that plugin exports `AddTwoInts` functions and its type is `func(int, int) int`.
@@ -47,11 +61,8 @@ result := plug.AddTwoInts(10, 20)
4761
```
4862
go get -u github.com/wendigo/go-bind-plugin
4963
go-bind-plugin -help
50-
```
5164
52-
Available flags:
5365
54-
```
5566
Usage of go-bind-plugin:
5667
-dereference-vars
5768
Dereference plugin variables

cli/template.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,24 @@ func Bind{{.Config.OutputName}}(path string) (*{{.Config.OutputName}}, error) {
9696
{{range .Plugin.Functions}}
9797
func{{ .Name }}, err := p.Lookup("{{ .Name }}")
9898
if err != nil {
99-
return nil, err
99+
return nil, fmt.Errorf("Could not import function '{{ .Name }}', symbol not found: %s", err)
100100
}
101101
102102
if typed, ok := func{{ .Name }}.({{ .Signature }}); ok {
103103
ret._{{ .Name }} = typed
104104
} else {
105-
return nil, fmt.Errorf("Could not export function '{{ .Name }}', incompatible types '{{ .Signature }}' and '%s'", reflect.TypeOf(func{{ .Name }}))
105+
return nil, fmt.Errorf("Could not import function '{{ .Name }}', incompatible types '{{ .Signature }}' and '%s'", reflect.TypeOf(func{{ .Name }}))
106106
}
107107
{{end}}{{range .Plugin.Variables}}
108108
var{{ .Name }}, err := p.Lookup("{{ .Name }}")
109109
if err != nil {
110-
return nil, err
110+
return nil, fmt.Errorf("Could not import variable '{{ .Name }}', symbol not found: %s", err)
111111
}
112112
113113
if typed, ok := var{{ .Name }}.(*{{.Signature}}); ok {
114114
ret.{{ .Name }} = {{if $useVarReference|not}}*{{end}}typed
115115
} else {
116-
return nil, fmt.Errorf("Could not export variable '{{ .Name }}', incompatible types '{{ .Signature }}' and '%s'", reflect.TypeOf(var{{ .Name }}))
116+
return nil, fmt.Errorf("Could not import variable '{{ .Name }}', incompatible types '{{ .Signature }}' and '%s'", reflect.TypeOf(var{{ .Name }}))
117117
}
118118
{{end}}
119119
return ret, nil

0 commit comments

Comments
 (0)