/
string_capture.go
186 lines (147 loc) · 4.77 KB
/
string_capture.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package transform
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/brexhq/substation/config"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
"github.com/brexhq/substation/message"
)
type stringCaptureConfig struct {
// Pattern is the regular expression used to capture values.
Pattern string `json:"pattern"`
re *regexp.Regexp
// Count is the number of captures to make.
//
// This is optional and defaults to 0, which means that a single
// capture is made. If a named capture group is used, then this
// is ignored.
Count int `json:"count"`
Object iconfig.Object `json:"object"`
}
func (c *stringCaptureConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *stringCaptureConfig) Validate() error {
if c.Object.SourceKey == "" && c.Object.TargetKey != "" {
return fmt.Errorf("object_source_key: %v", errors.ErrMissingRequiredOption)
}
if c.Object.SourceKey != "" && c.Object.TargetKey == "" {
return fmt.Errorf("object_target_key: %v", errors.ErrMissingRequiredOption)
}
if c.Pattern == "" {
return fmt.Errorf("pattern: %v", errors.ErrMissingRequiredOption)
}
re, err := regexp.Compile(c.Pattern)
if err != nil {
return fmt.Errorf("pattern: %v", err)
}
c.re = re
return nil
}
func newStringCapture(_ context.Context, cfg config.Config) (*stringCapture, error) {
conf := stringCaptureConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, fmt.Errorf("transform: string_capture: %v", err)
}
if err := conf.Validate(); err != nil {
return nil, fmt.Errorf("transform: string_capture: %v", err)
}
tf := stringCapture{
conf: conf,
isObject: conf.Object.SourceKey != "" && conf.Object.TargetKey != "",
// Check if the regular expression contains at least one named capture group.
containsCaptureGroup: strings.Contains(conf.Pattern, "(?P<"),
}
return &tf, nil
}
type stringCapture struct {
conf stringCaptureConfig
isObject bool
containsCaptureGroup bool
}
func (tf *stringCapture) Transform(_ context.Context, msg *message.Message) ([]*message.Message, error) {
if msg.IsControl() {
return []*message.Message{msg}, nil
}
if !tf.isObject {
switch {
case tf.containsCaptureGroup:
outMsg := message.New().SetMetadata(msg.Metadata())
matches := tf.conf.re.FindSubmatch(msg.Data())
for i, m := range matches {
if i == 0 {
continue
}
if err := outMsg.SetValue(tf.conf.re.SubexpNames()[i], m); err != nil {
return nil, fmt.Errorf("transform: string_capture: %v", err)
}
}
return []*message.Message{outMsg}, nil
case tf.conf.Count == 0:
matches := tf.conf.re.FindSubmatch(msg.Data())
msg.SetData(strCaptureGetBytesMatch(matches))
return []*message.Message{msg}, nil
default:
tmpMsg := message.New()
subs := tf.conf.re.FindAllSubmatch(msg.Data(), tf.conf.Count)
for _, s := range subs {
m := strCaptureGetBytesMatch(s)
if err := tmpMsg.SetValue("key.-1", m); err != nil {
return nil, fmt.Errorf("transform: string_capture: %v", err)
}
}
v := tmpMsg.GetValue("key")
msg.SetData(v.Bytes())
return []*message.Message{msg}, nil
}
}
value := msg.GetValue(tf.conf.Object.SourceKey)
if !value.Exists() {
return []*message.Message{msg}, nil
}
switch {
case tf.containsCaptureGroup:
matches := tf.conf.re.FindStringSubmatch(value.String())
for i, match := range matches {
if i == 0 {
continue
}
// If the same key is used multiple times, then this will correctly
// set multiple named groups into that key.
//
// If set_key is "a" and the first group returns {"b":"c"}, then
// the output is {"a":{"b":"c"}}. If the second group returns
// {"d":"e"} then the output is {"a":{"b":"c","d":"e"}}.
setKey := tf.conf.Object.TargetKey + "." + tf.conf.re.SubexpNames()[i]
if err := msg.SetValue(setKey, match); err != nil {
return nil, fmt.Errorf("transform: string_capture: %v", err)
}
}
return []*message.Message{msg}, nil
case tf.conf.Count == 0:
matches := tf.conf.re.FindStringSubmatch(value.String())
if err := msg.SetValue(tf.conf.Object.TargetKey, strCaptureGetStringMatch(matches)); err != nil {
return nil, fmt.Errorf("transform: string_capture: %v", err)
}
return []*message.Message{msg}, nil
default:
var matches []string
subs := tf.conf.re.FindAllStringSubmatch(value.String(), tf.conf.Count)
for _, s := range subs {
m := strCaptureGetStringMatch(s)
matches = append(matches, m)
}
if err := msg.SetValue(tf.conf.Object.TargetKey, matches); err != nil {
return nil, fmt.Errorf("transform: string_capture: %v", err)
}
return []*message.Message{msg}, nil
}
}
func (tf *stringCapture) String() string {
b, _ := json.Marshal(tf.conf)
return string(b)
}