-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_split.go
More file actions
68 lines (60 loc) · 1.73 KB
/
Copy pathstream_split.go
File metadata and controls
68 lines (60 loc) · 1.73 KB
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
package caravan
import (
_stream "github.com/kode4food/caravan/internal/stream"
"github.com/kode4food/caravan/internal/stream/node"
"github.com/kode4food/caravan/stream"
"github.com/kode4food/caravan/stream/context"
"github.com/kode4food/caravan/topic"
)
// StreamBranch builds one branch of a split Stream
type StreamBranch[In, Out any] struct {
processor stream.Processor[In, Out]
}
// Branch starts building a branch for Split
func (p StreamPipeline[Msg]) Branch() StreamBranch[Msg, Msg] {
return StreamBranch[Msg, Msg]{
processor: func(c *context.Context[Msg, Msg]) {
for {
msg, ok := c.FetchMessage()
if !ok || !c.ForwardResult(msg) {
return
}
}
},
}
}
// Filter filters a Stream branch
func (b StreamBranch[In, Msg]) Filter(
fn func(Msg) bool,
) StreamBranch[In, Msg] {
return b.process(node.Filter(fn))
}
// Map transforms messages in a Stream branch
func (b StreamBranch[In, From]) Map[To any](
fn func(From) To,
) StreamBranch[In, To] {
return b.process(node.Map(fn))
}
// TopicProducer sends Stream branch messages to a Topic
func (b StreamBranch[In, Msg]) TopicProducer(
t topic.Topic[Msg],
) StreamBranch[In, Msg] {
return b.process(node.TopicProducer(t))
}
// Split sends each message through every provided branch
func (p StreamPipeline[Msg]) Split[Out any](
branches ...StreamBranch[Msg, Out],
) stream.Stream {
processors := make([]stream.Processor[Msg, Out], len(branches))
for i, branch := range branches {
processors[i] = branch.processor
}
return p.process(node.Split(processors...)).Build()
}
func (b StreamBranch[In, From]) process[To any](
processor stream.Processor[From, To],
) StreamBranch[In, To] {
return StreamBranch[In, To]{
processor: _stream.Bind(b.processor, processor),
}
}