-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.go
94 lines (73 loc) · 2.01 KB
/
processor.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
package ibt
import (
"context"
"errors"
"sort"
"github.com/teamjorge/ibt/headers"
"github.com/teamjorge/ibt/utilities"
)
type Processor interface {
Process(input Tick, hasNext bool, session *headers.Session) error
Whitelist() []string
}
func Process(ctx context.Context, stubs StubGroup, processors ...Processor) error {
sort.Sort(stubs)
for _, stub := range stubs {
if err := process(ctx, stub, processors...); err != nil {
return err
}
}
return nil
}
func process(ctx context.Context, stub Stub, processors ...Processor) error {
header := stub.header
whitelist := buildWhitelist(header.VarHeader, processors...)
parser := NewParser(stub.r, header, whitelist...)
for {
select {
case <-ctx.Done():
return errors.New("context cancelled")
default:
}
tick, hasNext := parser.Next()
for _, proc := range processors {
if err := proc.Process(tick.Filter(proc.Whitelist()...), hasNext, header.SessionInfo); err != nil {
return err
}
}
if !hasNext {
break
}
}
return nil
}
// getcinoketeWhitelist compiles the whitelists from all processors and removes overlap
func buildWhitelist(vars map[string]headers.VarHeader, processors ...Processor) []string {
whitelist := make([]string, 0)
for _, proc := range processors {
whitelist = append(whitelist, parseAndValidateWhitelist(vars, proc)...)
}
return utilities.GetDistinct(whitelist)
}
// parseWhitelist will retrieve vars when * is used and ensure a unique list
//
// Variables that are not found in the VarHeader will automatically be excluded.
func parseAndValidateWhitelist(vars map[string]headers.VarHeader, processor Processor) []string {
whitelist := processor.Whitelist()
if len(whitelist) == 0 {
return headers.AvailableVars(vars)
}
for _, col := range whitelist {
if col == "*" {
return headers.AvailableVars(vars)
}
}
columns := make([]string, 0)
// Ensure only valid columns are added
for _, col := range whitelist {
if _, ok := vars[col]; ok {
columns = append(columns, col)
}
}
return columns
}