/
static-plugin.go
63 lines (53 loc) · 1.53 KB
/
static-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
51
52
53
54
55
56
57
58
59
60
61
62
63
package plugin
import (
"context"
"github.com/aperturerobotics/controllerbus/bus"
"github.com/aperturerobotics/controllerbus/controller"
)
// FactoryCtorSet is a list of factory constructors.
type FactoryCtorSet = func(b bus.Bus) []controller.Factory
// StaticPlugin contains a compiled set of controller factories.
type StaticPlugin struct {
binaryID string
binaryVersion string
factories FactoryCtorSet
}
// NewStaticPlugin constructs a new static plugin.
func NewStaticPlugin(
binaryID string,
binaryVersion string,
factories FactoryCtorSet,
) *StaticPlugin {
return &StaticPlugin{
binaryID: binaryID,
binaryVersion: binaryVersion,
factories: factories,
}
}
// GetBinaryID returns the plugin binary ID.
// Usually the go.mod package name.
func (e *StaticPlugin) GetBinaryID() string {
return e.binaryID
}
// GetBinaryVersion returns the plugin binary version
// Does not need to be semver (usually uses Go.mod versioning)
func (e *StaticPlugin) GetBinaryVersion() string {
return e.binaryVersion
}
// NewPluginResolver constructs the resolver and inits the plugin.
// ctx is canceled when the plugin is about to be unloaded.
func (e *StaticPlugin) NewPluginResolver(ctx context.Context, b bus.Bus) (PluginResolver, error) {
return NewResolver(
ctx,
b,
e.GetBinaryID(),
e.GetBinaryVersion(),
e.factories(b)...,
), nil
}
// PrePluginUnload is called just before the plugin is unloaded.
func (e *StaticPlugin) PrePluginUnload() {
// noop
}
// _ is a type assertion
var _ Plugin = ((*StaticPlugin)(nil))