/
controller_factory.go
61 lines (48 loc) · 1.52 KB
/
controller_factory.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
package main
import (
"context"
"errors"
"github.com/aperturerobotics/controllerbus/config"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/blang/semver"
)
// toyFactoryVersion is the compiled-in version
var toyFactoryVersion = semver.MustParse("0.1.0")
// toyControllerID is the controller id
const toyControllerID = "hello-world"
// ToyFactory implements the toy controller factory.
type ToyFactory struct{}
// NewToyFactory builds a new generic factory.
func NewToyFactory() *ToyFactory {
return &ToyFactory{}
}
// GetControllerID returns the unique ID for the controller.
func (f *ToyFactory) GetControllerID() string {
return toyControllerID
}
// GetConfigID returns the unique ID for the controller config.
func (f *ToyFactory) GetConfigID() string {
return f.GetControllerID()
}
// ConstructConfig constructs an instance of the controller configuration.
func (f *ToyFactory) ConstructConfig() config.Config {
return &ToyControllerConfig{}
}
// Construct constructs the associated controller given configuration.
func (f *ToyFactory) Construct(
ctx context.Context,
c config.Config,
opts controller.ConstructOpts,
) (controller.Controller, error) {
conf, ok := c.(*ToyControllerConfig)
if !ok {
return nil, errors.New("wrong type of config")
}
return NewToyController(opts.GetLogger(), conf)
}
// GetVersion returns the version of this controller.
func (f *ToyFactory) GetVersion() semver.Version {
return toyFactoryVersion
}
// _ is a type assertion
var _ controller.Factory = ((*ToyFactory)(nil))