-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreplace.go
More file actions
42 lines (32 loc) · 899 Bytes
/
replace.go
File metadata and controls
42 lines (32 loc) · 899 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
package replace
import (
"context"
"regexp"
"github.com/patterninc/caterpillar/internal/pkg/pipeline/record"
"github.com/patterninc/caterpillar/internal/pkg/pipeline/task"
)
type replace struct {
task.Base `yaml:",inline" json:",inline"`
Expression string `yaml:"expression,omitempty" json:"expression,omitempty"`
Replacement string `yaml:"replacement,omitempty" json:"replacement,omitempty"`
}
func New() (task.Task, error) {
return &replace{}, nil
}
func (r *replace) Run(ctx context.Context, input <-chan *record.Record, output chan<- *record.Record) (err error) {
rx, err := regexp.Compile(r.Expression)
if err != nil {
return err
}
replacementBytes := []byte(r.Replacement)
if output != nil {
for {
record, ok := r.GetRecord(input)
if !ok {
break
}
r.SendData(record.Meta, rx.ReplaceAll(record.Data, replacementBytes), output)
}
}
return nil
}