-
Notifications
You must be signed in to change notification settings - Fork 19
/
processor_factory.go
44 lines (36 loc) · 1.43 KB
/
processor_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
package processors
import (
"fmt"
)
// ProcessorConstructor must be implemented by each Processor.
// It provides a basic no-arg constructor for instances of an ProcessorImpl.
type ProcessorConstructor interface {
// New should return an instantiation of a Processor.
// Configuration values should be passed and can be processed during `Init()`.
New() Processor
}
// ProcessorConstructorFunc is Constructor implementation for processors
type ProcessorConstructorFunc func() Processor
// New initializes a processor constructor
func (f ProcessorConstructorFunc) New() Processor {
return f()
}
// Processors are the constructors to build processor plugins.
var Processors = make(map[string]ProcessorConstructor)
// Register is used to register ProcessorConstructor implementations. This mechanism allows
// for loose coupling between the configuration and the implementation. It is extremely similar to the way sql.DB
// drivers are configured and used.
func Register(name string, constructor ProcessorConstructor) {
if _, ok := Processors[name]; ok {
panic(fmt.Errorf("processor %s already registered", name))
}
Processors[name] = constructor
}
// ProcessorConstructorByName returns a Processor constructor for the name provided
func ProcessorConstructorByName(name string) (ProcessorConstructor, error) {
constructor, ok := Processors[name]
if !ok {
return nil, fmt.Errorf("no Processor Constructor for %s", name)
}
return constructor, nil
}