-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplit.go
More file actions
45 lines (34 loc) · 829 Bytes
/
split.go
File metadata and controls
45 lines (34 loc) · 829 Bytes
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
package split
import (
"bytes"
"context"
"github.com/patterninc/caterpillar/internal/pkg/pipeline/record"
"github.com/patterninc/caterpillar/internal/pkg/pipeline/task"
)
const (
defaultDelimiter = "\n"
)
type split struct {
task.Base `yaml:",inline" json:",inline"`
Delimiter string `yaml:"delimiter,omitempty" json:"delimiter,omitempty"`
}
func New() (task.Task, error) {
return &split{
Delimiter: defaultDelimiter,
}, nil
}
func (s *split) Run(ctx context.Context, input <-chan *record.Record, output chan<- *record.Record) error {
delimBytes := []byte(s.Delimiter)
for {
r, ok := s.GetRecord(input)
if !ok {
break
}
data := bytes.TrimSuffix(r.Data, delimBytes)
lines := bytes.Split(data, delimBytes)
for _, line := range lines {
s.SendData(r.Meta, line, output)
}
}
return nil
}