-
Notifications
You must be signed in to change notification settings - Fork 2
/
parameters.go
120 lines (105 loc) · 2.52 KB
/
parameters.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
package engine
import (
"fmt"
"strings"
"sync"
"time"
)
const (
errParameterAlreadyExists = "parameter '%s' already exists"
errNameNotDeclared = "parameter '%s' needs to be declared before it can be used"
ParameterTableName = "PARAMETERS"
)
type ParameterTable struct {
sync.Mutex
allowedNames map[string]bool
values map[string]interface{}
}
func NewParameterTable() *ParameterTable {
return &ParameterTable{
allowedNames: make(map[string]bool),
values: make(map[string]interface{}),
}
}
func (p *ParameterTable) Declare(name string) error {
p.Lock()
defer p.Unlock()
if p.allowedNames[strings.ToLower(name)] {
return fmt.Errorf(errParameterAlreadyExists, name)
}
p.allowedNames[strings.ToLower(name)] = true
return nil
}
func (p *ParameterTable) Get(name string) (interface{}, bool) {
p.Lock()
defer p.Unlock()
val, ok := p.values[strings.ToLower(name)]
return val, ok
}
func (p *ParameterTable) Set(name string, value interface{}) error {
p.Lock()
defer p.Unlock()
if !p.allowedNames[strings.ToLower(name)] {
return fmt.Errorf(errNameNotDeclared, name)
}
p.values[strings.ToLower(name)] = value
return nil
}
/**
type Destination interface {
//Ping checks that the destination is available. It is used to verify
//the destination at runtime.
Ping() error
//Open gives the destination a stream to start pulling from and an error stream
Open(Stream, Logger, Stopper)
}
*/
type ParameterTableDestination struct {
cols []string
p *ParameterTable
}
func NewParameterTableDestination(p *ParameterTable, cols []string) Destination {
return &ParameterTableDestination{
p: p,
cols: cols,
}
}
func (p *ParameterTableDestination) Ping() error {
return nil
}
func (p *ParameterTableDestination) Open(s Stream, l Logger, st Stopper) {
l.Chan() <- Event{
Source: "Parameter Table",
Level: Trace,
Time: time.Now(),
Message: "Parameter table opened",
}
var cols []string
for msg := range s.Chan(ParameterTableName) {
if st.Stopped() {
return
}
if cols == nil {
cols = s.Columns()
}
for i := range msg.Data {
if len(msg.Data) != len(p.cols) {
l.Chan() <- Event{
Source: "Parameter Table",
Level: Error,
Time: time.Now(),
Message: fmt.Sprintf("Expected %v parameters but got %v", len(p.cols), len(msg.Data)),
}
}
err := p.p.Set(p.cols[i], msg.Data[i])
if err != nil {
l.Chan() <- Event{
Source: "Parameter Table",
Level: Error,
Time: time.Now(),
Message: err.Error(),
}
}
}
}
}