/
datasource.go
72 lines (64 loc) · 1.64 KB
/
datasource.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
package datasource
import (
"fmt"
"io"
"go.uber.org/multierr"
)
// The generic interface to describe the datasource
// Each DataSource instance listen in one property type.
type DataSource interface {
// Add specified property handler in current datasource
AddPropertyHandler(h PropertyHandler)
// Remove specified property handler in current datasource
RemovePropertyHandler(h PropertyHandler)
// Read original data from the data source.
// return source bytes if succeed to read, if not, return error when reading
ReadSource() ([]byte, error)
// Initialize the datasource and load initial rules
// start listener to listen on dynamic source
// return error if initialize failed;
// once initialized, listener should recover all panic and error.
Initialize() error
// Close the data source.
io.Closer
}
type Base struct {
handlers []PropertyHandler
}
func (b *Base) Handle(src []byte) (err error) {
for _, h := range b.handlers {
e := h.Handle(src)
if e != nil {
err = multierr.Append(err, e)
}
}
if err == nil {
return nil
}
return Error{code: HandleSourceError, desc: fmt.Sprintf("%+v", err)}
}
// return idx if existed, else return -1
func (b *Base) indexOfHandler(h PropertyHandler) int {
for idx, handler := range b.handlers {
if handler == h {
return idx
}
}
return -1
}
func (b *Base) AddPropertyHandler(h PropertyHandler) {
if h == nil || b.indexOfHandler(h) >= 0 {
return
}
b.handlers = append(b.handlers, h)
}
func (b *Base) RemovePropertyHandler(h PropertyHandler) {
if h == nil {
return
}
idx := b.indexOfHandler(h)
if idx < 0 {
return
}
b.handlers = append(b.handlers[:idx], b.handlers[idx+1:]...)
}