This repository was archived by the owner on Aug 14, 2023. It is now read-only.
generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
91 lines (77 loc) · 2.29 KB
/
plugin.go
File metadata and controls
91 lines (77 loc) · 2.29 KB
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
package pluginfx
import (
"errors"
"fmt"
"plugin"
)
// Plugin defines the behavior of something that can look up
// exported symbols. *plugin.Plugin implements this interface.
type Plugin interface {
// Lookup returns the value of the given symbol, or an error
// if no such symbol exists.
//
// The *plugin.Plugin type returns a generated error from this method,
// making error disambiguation hard or impossible. The Lookup function
// in this package helps with that by ensuring that a *MissingSymbolError
// is returned in cases where a symbol cannot be found.
Lookup(string) (plugin.Symbol, error)
}
// OpenError is returned by Open to indicate that a source of symbols could not be loaded.
type OpenError struct {
Path string
Err error
}
func (oe *OpenError) Unwrap() error {
return oe.Err
}
func (oe *OpenError) Error() string {
return fmt.Sprintf("Unable to load plugin from path %s: %s", oe.Path, oe.Err)
}
// Open loads a Plugin from a path. This is the analog to plugin.Open,
// and returns a *OpenError instead of a generated error.
func Open(path string) (Plugin, error) {
p, err := plugin.Open(path)
if err != nil {
err = &OpenError{
Path: path,
Err: err,
}
}
return p, err
}
// MissingSymbolError indicates that a symbol was not found. This error is returned
// by Lookup to normalize errors coming from plugins.
type MissingSymbolError struct {
Name string
Err error
}
func (mse *MissingSymbolError) Unwrap() error {
return mse.Err
}
func (mse *MissingSymbolError) Error() string {
if mse.Err != nil {
return fmt.Sprintf("Missing symbol %s: %s", mse.Name, mse.Err)
}
return fmt.Sprintf("Missing symbol %s", mse.Name)
}
// IsMissingSymbolError tests err to see if it is a *MissingSymbolError.
// This function is a shorthand for situations where calling code only needs
// to be aware that an error indicated a symbol was missing.
func IsMissingSymbolError(err error) bool {
var mse *MissingSymbolError
return errors.As(err, &mse)
}
// Lookup invokes s.Lookup and normalizes any error to *MissingSymbolError.
func Lookup(p Plugin, name string) (interface{}, error) {
symbol, err := p.Lookup(name)
if err != nil {
var mse *MissingSymbolError
if !errors.As(err, &mse) {
err = &MissingSymbolError{
Name: name,
Err: err,
}
}
}
return symbol, err
}