Skip to content

Commit

Permalink
Merge pull request #314 from JeanMertz/regexp-expand
Browse files Browse the repository at this point in the history
Add regexp_expand text operator
  • Loading branch information
Jeffail committed Nov 15, 2019
2 parents 0a9c612 + 7ad495c commit 8303e63
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/processors/README.md
Expand Up @@ -1853,6 +1853,12 @@ Prepends text to the beginning of the payload.
Returns a doubled-quoted string, using escape sequences (\t, \n, \xFF, \u0100)
for control characters and other non-printable characters.

#### `regexp_expand`

Expands the template variables with the matched occurrences of the regular
expression in a message. Inside the value $ signs are interpreted as submatch
expansions, e.g. $1 represents the text of the first submatch.

#### `replace`

Replaces all occurrences of the argument in a message with a value.
Expand Down
22 changes: 22 additions & 0 deletions lib/processor/text.go
Expand Up @@ -85,6 +85,12 @@ Prepends text to the beginning of the payload.
Returns a doubled-quoted string, using escape sequences (\t, \n, \xFF, \u0100)
for control characters and other non-printable characters.
#### ` + "`regexp_expand`" + `
Expands the template variables with the matched occurrences of the regular
expression in a message. Inside the value $ signs are interpreted as submatch
expansions, e.g. $1 represents the text of the first submatch.
#### ` + "`replace`" + `
Replaces all occurrences of the argument in a message with a value.
Expand Down Expand Up @@ -219,6 +225,20 @@ func newTextSetOperator() textOperator {
}
}

func newTextRegexpExpandOperator(arg string) (textOperator, error) {
rp, err := regexp.Compile(arg)
if err != nil {
return nil, err
}
return func(body []byte, value []byte) ([]byte, error) {
var result []byte
for _, submatches := range rp.FindAllSubmatchIndex(body, -1) {
result = rp.Expand(result, value, body, submatches)
}
return result, nil
}, nil
}

func newTextReplaceOperator(arg string) textOperator {
replaceArg := []byte(arg)
return func(body []byte, value []byte) ([]byte, error) {
Expand Down Expand Up @@ -277,6 +297,8 @@ func getTextOperator(opStr string, arg string) (textOperator, error) {
return newTextPrependOperator(), nil
case "quote":
return newTextQuoteOperator(), nil
case "regexp_expand":
return newTextRegexpExpandOperator(arg)
case "replace":
return newTextReplaceOperator(arg), nil
case "replace_regexp":
Expand Down
64 changes: 64 additions & 0 deletions lib/processor/text_test.go
Expand Up @@ -634,6 +634,70 @@ func TestTextTrim(t *testing.T) {
}
}

func TestTextRegexpExpand(t *testing.T) {
tLog := log.New(os.Stdout, log.Config{LogLevel: "NONE"})
tStats := metrics.DudType{}

type jTest struct {
name string
arg string
value string
input string
output string
}

tests := []jTest{
{
name: "regexp expand 1",
arg: "(foo) bar",
value: "$1",
input: `foo bar`,
output: `foo`,
},
{
name: "regexp expand 2",
arg: "(?P<key>\\w+) \\w+",
value: "$key baz",
input: `foo bar`,
output: `foo baz`,
},
{
name: "regexp expand 3",
arg: "(?m)(?P<key>\\w+):\\s+(?P<value>\\w+)$",
value: "$key=$value\n",
input: "# comment line\nfoo1: bar1\nbar2: baz2\n\n# another comment line\nbaz3: qux3",
output: "foo1=bar1\nbar2=baz2\nbaz3=qux3\n",
},
}

for _, test := range tests {
conf := NewConfig()
conf.Text.Operator = "regexp_expand"
conf.Text.Arg = test.arg
conf.Text.Value = test.value
conf.Text.Parts = []int{0}

tp, err := NewText(conf, nil, tLog, tStats)
if err != nil {
t.Fatalf("Error for test '%v': %v", test.name, err)
}

inMsg := message.New(
[][]byte{
[]byte(test.input),
},
)
msgs, _ := tp.ProcessMessage(inMsg)
if len(msgs) != 1 {
t.Fatalf("Test '%v' did not succeed", test.name)
}

if exp, act := test.output, string(message.GetAllBytes(msgs[0])[0]); exp != act {
t.Errorf("Wrong result '%v': %v != %v", test.name, act, exp)
}
}
}

func TestTextReplace(t *testing.T) {
tLog := log.New(os.Stdout, log.Config{LogLevel: "NONE"})
tStats := metrics.DudType{}
Expand Down

0 comments on commit 8303e63

Please sign in to comment.