-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransform.go
59 lines (50 loc) · 1.57 KB
/
transform.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
package frizzle
import (
"bytes"
)
// Transform is a function that modifies a Msg
type Transform func(Msg) Msg
// FrizTransformer provides a Transform to apply when a Msg is sent or received
type FrizTransformer interface {
SendTransform() Transform
ReceiveTransform() Transform
}
// WithTransformer returns an Option to add the provided FrizTransformer to a *Friz
func WithTransformer(ft FrizTransformer) Option {
return func(f *Friz) {
f.tforms = append(f.tforms, ft)
}
}
var (
_ FrizTransformer = (*SimpleSepTransformer)(nil)
)
// SimpleSepTransformer appends and removes a specified separator such as '\n' at the end of the Msg
type SimpleSepTransformer struct {
sep []byte
}
// SendTransform returns a Transform to append the separator if it is not present at the end of Msg.Data()
func (st *SimpleSepTransformer) SendTransform() Transform {
return func(m Msg) Msg {
d := m.Data()
if !bytes.Equal(d[len(d)-len(st.sep):], st.sep) {
d = append(d, st.sep...)
}
return NewSimpleMsg(m.ID(), d, m.Timestamp())
}
}
// ReceiveTransform returns a Transform to remove the separator if it is present at the end of Msg.Data()
func (st *SimpleSepTransformer) ReceiveTransform() Transform {
return func(m Msg) Msg {
d := m.Data()
if bytes.Equal(d[len(d)-len(st.sep):], st.sep) {
d = d[0 : len(d)-len(st.sep)]
}
return NewSimpleMsg(m.ID(), d, m.Timestamp())
}
}
// NewSimpleSepTransformer initializes a new SepTransformer with a specified separator
func NewSimpleSepTransformer(sep []byte) FrizTransformer {
return FrizTransformer(&SimpleSepTransformer{
sep: sep,
})
}