-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugins.go
117 lines (90 loc) · 2.04 KB
/
plugins.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cypress
import (
"errors"
"sort"
"strings"
)
var (
// Indicates that a Plugin does not have a Receiver
ErrNoReceiver = errors.New("no receiver available")
// Indicates that a Plugin does not have a Generator
ErrNoGenerator = errors.New("no generator available")
)
// An interface implemented by plugins used by the router
type Plugin interface{}
type ReceiverPlugin interface {
Receiver() (Receiver, error)
}
type GeneratorPlugin interface {
Generator() (Generator, error)
}
type FiltererPlugin interface {
Filterer() (Filterer, error)
}
var plugins = map[string]func() Plugin{}
// Add a new plugin by name with a function to create a new instance
// of this plugin.
func AddPlugin(name string, creator func() Plugin) {
plugins[strings.ToLower(name)] = creator
}
// Find a plugin by name and invoke it's creator function to create
// a new Plugin instance
func FindPlugin(name string) (Plugin, bool) {
t, ok := plugins[strings.ToLower(name)]
if !ok {
return nil, false
}
return t(), true
}
func AllPlugins() []string {
var s []string
for name, _ := range plugins {
s = append(s, name)
}
sort.Strings(s)
return s
}
// Used for testing only
type TestPlugin struct {
Messages chan *Message
FilterFields map[string]interface{}
}
func (t *TestPlugin) Description() string {
return "<internal>"
}
func (t *TestPlugin) Init() {
t.Messages = make(chan *Message, 10)
}
func (t *TestPlugin) Generator() (Generator, error) {
return t, nil
}
func (t *TestPlugin) Generate() (*Message, error) {
return <-t.Messages, nil
}
func (t *TestPlugin) Receiver() (Receiver, error) {
return t, nil
}
func (t *TestPlugin) Receive(m *Message) error {
t.Messages <- m
return nil
}
func (t *TestPlugin) Filterer() (Filterer, error) {
return t, nil
}
func (t *TestPlugin) Filter(m *Message) (*Message, error) {
for k, v := range t.FilterFields {
m.Add(k, v)
}
return m, nil
}
func (t *TestPlugin) Close() error {
close(t.Messages)
return nil
}
func init() {
AddPlugin("Test", func() Plugin {
t := &TestPlugin{}
t.Init()
return t
})
}