-
Notifications
You must be signed in to change notification settings - Fork 3
/
registry.go
237 lines (203 loc) · 6.78 KB
/
registry.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package reg
import (
"fmt"
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/bitflow/fork"
)
const (
MultiplexForkName = "multiplex"
)
type AnalysisFunc func(pipeline *bitflow.SamplePipeline, params map[string]interface{}) error
type BatchStepFunc func(params map[string]interface{}) (bitflow.BatchProcessingStep, error)
type ForkFunc func(subpiplines []Subpipeline, params map[string]interface{}) (fork.Distributor, error)
type RegisteredParameters map[string]RegisteredParameter
type RegisteredParameter struct {
Name string
Parser ParameterParser
Default interface{}
Required bool
}
type RegisteredStep struct {
Name string
Description string
Params RegisteredParameters
}
type RegisteredPipelineStep struct {
RegisteredStep
Func AnalysisFunc
}
type RegisteredBatchStep struct {
RegisteredStep
Func BatchStepFunc
}
type RegisteredFork struct {
RegisteredStep
Func ForkFunc
}
type Subpipeline interface {
Build() (*bitflow.SamplePipeline, error)
Keys() []string
}
// Avoid having to specify a pointer to a ProcessorRegistry at many locations.
type ProcessorRegistry struct {
*ProcessorRegistryImpl
}
type ProcessorRegistryImpl struct {
Endpoints bitflow.EndpointFactory
stepRegistry map[string]*RegisteredPipelineStep
batchRegistry map[string]*RegisteredBatchStep
forkRegistry map[string]*RegisteredFork
}
func NewProcessorRegistry() ProcessorRegistry {
reg := ProcessorRegistry{
ProcessorRegistryImpl: &ProcessorRegistryImpl{
Endpoints: *bitflow.NewEndpointFactory(),
stepRegistry: make(map[string]*RegisteredPipelineStep),
batchRegistry: make(map[string]*RegisteredBatchStep),
forkRegistry: make(map[string]*RegisteredFork),
},
}
RegisterMultiplexFork(reg)
return reg
}
func (r *ProcessorRegistryImpl) GetStep(name string) *RegisteredPipelineStep {
return r.stepRegistry[name]
}
func (r *ProcessorRegistryImpl) GetFork(name string) *RegisteredFork {
return r.forkRegistry[name]
}
func (r *ProcessorRegistryImpl) GetBatchStep(name string) *RegisteredBatchStep {
return r.batchRegistry[name]
}
func (r *RegisteredStep) Param(name string, parser ParameterParser, defaultValue interface{}, isRequired bool) *RegisteredStep {
r.Params.Param(name, parser, defaultValue, isRequired)
return r
}
func (r *RegisteredStep) Optional(name string, parser ParameterParser, defaultValue interface{}) *RegisteredStep {
r.Params.Optional(name, parser, defaultValue)
return r
}
func (r *RegisteredStep) Required(name string, parser ParameterParser) *RegisteredStep {
r.Params.Required(name, parser)
return r
}
func (params RegisteredParameters) Param(name string, parser ParameterParser, defaultValue interface{}, isRequired bool) RegisteredParameters {
if _, ok := params[name]; ok {
panic(fmt.Sprintf("Parameter %v already registered", name))
}
if !isRequired && !parser.CorrectType(defaultValue) {
panic(fmt.Sprintf("Default value for parameter '%v' (type %v) is of unexpected type %T: %v", name, parser, defaultValue, defaultValue))
}
params[name] = RegisteredParameter{
Name: name,
Parser: parser,
Default: defaultValue,
Required: isRequired,
}
return params
}
func (params RegisteredParameters) Optional(name string, parser ParameterParser, defaultValue interface{}) RegisteredParameters {
return params.Param(name, parser, defaultValue, false)
}
func (params RegisteredParameters) Required(name string, parser ParameterParser) RegisteredParameters {
return params.Param(name, parser, nil, true)
}
func (params RegisteredParameters) ParsePrimitives(stringParams map[string]string) (map[string]interface{}, error) {
result := make(map[string]interface{}, len(stringParams))
for key, val := range stringParams {
param, ok := params[key]
if !ok {
return nil, fmt.Errorf("Unexpected parameter %v = %v", key, val)
}
var err error
result[key], err = param.Parser.ParsePrimitive(val)
if err != nil {
return nil, err
}
}
return result, params.ValidateAndSetDefaults(result)
}
// Check if required parameters are defined and fill defaults for optional parameters
func (params RegisteredParameters) ValidateAndSetDefaults(parsedValues map[string]interface{}) error {
for key, param := range params {
if _, paramDefined := parsedValues[key]; !paramDefined {
if param.Required {
return fmt.Errorf("Missing required parameter '%v' (type %v)", key, param.Parser)
} else {
parsedValues[key] = param.Default
}
}
if val := parsedValues[key]; !param.Parser.CorrectType(val) {
return fmt.Errorf("Value for parameter '%v' (type %v) is of unexpected type %T: %v", key, param.Parser, val, val)
}
}
return nil
}
func (r *RegisteredParameter) String() string {
defaultStr := ""
if r.Default != nil {
defaultStr = fmt.Sprintf(", default: %v", r.Default)
}
return fmt.Sprintf("%v (%v%v)", r.Name, r.Parser, defaultStr)
}
func (r *ProcessorRegistryImpl) RegisterStep(name string, setupPipeline AnalysisFunc, description string) *RegisteredStep {
if _, ok := r.stepRegistry[name]; ok {
panic("Analysis already registered: " + name)
}
newStep := &RegisteredPipelineStep{
RegisteredStep: RegisteredStep{
Name: name,
Description: description,
Params: make(RegisteredParameters),
},
Func: setupPipeline,
}
r.stepRegistry[name] = newStep
return &newStep.RegisteredStep
}
func (r *ProcessorRegistryImpl) RegisterFork(name string, createFork ForkFunc, description string) *RegisteredStep {
if _, ok := r.forkRegistry[name]; ok {
panic("Fork already registered: " + name)
}
newFork := &RegisteredFork{
RegisteredStep: RegisteredStep{
Name: name,
Description: description,
Params: make(RegisteredParameters),
},
Func: createFork,
}
r.forkRegistry[name] = newFork
return &newFork.RegisteredStep
}
func (r *ProcessorRegistryImpl) RegisterBatchStep(name string, createBatchStep BatchStepFunc, description string) *RegisteredStep {
if _, ok := r.batchRegistry[name]; ok {
panic("Batch step already registered: " + name)
}
newBatchStep := &RegisteredBatchStep{
RegisteredStep: RegisteredStep{
Name: name,
Description: description,
Params: make(RegisteredParameters),
},
Func: createBatchStep,
}
r.batchRegistry[name] = newBatchStep
return &newBatchStep.RegisteredStep
}
func RegisterMultiplexFork(builder ProcessorRegistry) {
builder.RegisterFork(MultiplexForkName, createMultiplexFork,
"Basic fork forwarding samples to all subpipelines. Subpipeline keys are ignored.")
}
func createMultiplexFork(subpipelines []Subpipeline, _ map[string]interface{}) (fork.Distributor, error) {
var res fork.MultiplexDistributor
res.Subpipelines = make([]*bitflow.SamplePipeline, len(subpipelines))
var err error
for i, pipe := range subpipelines {
res.Subpipelines[i], err = pipe.Build()
if err != nil {
return nil, err
}
}
return &res, nil
}