Skip to content
This repository was archived by the owner on Mar 21, 2023. It is now read-only.

Commit 4fcaf8d

Browse files
joschikroepke
authored andcommitted
Add concat() function (#20)
1 parent 2055a87 commit 4fcaf8d

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* This file is part of Graylog Pipeline Processor.
3+
*
4+
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog.plugins.pipelineprocessor.functions.strings;
18+
19+
import com.google.common.base.Strings;
20+
import org.graylog.plugins.pipelineprocessor.EvaluationContext;
21+
import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction;
22+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs;
23+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
24+
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
25+
26+
import static com.google.common.collect.ImmutableList.of;
27+
28+
public class Concat extends AbstractFunction<String> {
29+
public static final String NAME = "concat";
30+
private final ParameterDescriptor<String, String> firstParam;
31+
private final ParameterDescriptor<String, String> secondParam;
32+
33+
public Concat() {
34+
firstParam = ParameterDescriptor.string("first").build();
35+
secondParam = ParameterDescriptor.string("second").build();
36+
}
37+
38+
@Override
39+
public String evaluate(FunctionArgs args, EvaluationContext context) {
40+
final String first = Strings.nullToEmpty(firstParam.required(args, context));
41+
final String second = Strings.nullToEmpty(secondParam.required(args, context));
42+
43+
return first.concat(second);
44+
}
45+
46+
@Override
47+
public FunctionDescriptor<String> descriptor() {
48+
return FunctionDescriptor.<String>builder()
49+
.name(NAME)
50+
.returnType(String.class)
51+
.params(of(firstParam, secondParam))
52+
.build();
53+
}
54+
}

src/test/java/org/graylog/plugins/pipelineprocessor/functions/FunctionsSnippetsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.graylog.plugins.pipelineprocessor.functions.messages.SetFields;
5555
import org.graylog.plugins.pipelineprocessor.functions.strings.Abbreviate;
5656
import org.graylog.plugins.pipelineprocessor.functions.strings.Capitalize;
57+
import org.graylog.plugins.pipelineprocessor.functions.strings.Concat;
5758
import org.graylog.plugins.pipelineprocessor.functions.strings.Contains;
5859
import org.graylog.plugins.pipelineprocessor.functions.strings.GrokMatch;
5960
import org.graylog.plugins.pipelineprocessor.functions.strings.Lowercase;
@@ -137,6 +138,7 @@ public static void registerFunctions() {
137138
// string functions
138139
functions.put(Abbreviate.NAME, new Abbreviate());
139140
functions.put(Capitalize.NAME, new Capitalize());
141+
functions.put(Concat.NAME, new Concat());
140142
functions.put(Contains.NAME, new Contains());
141143
functions.put(Lowercase.NAME, new Lowercase());
142144
functions.put(Substring.NAME, new Substring());

src/test/resources/org/graylog/plugins/pipelineprocessor/functions/strings.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ when
1313
abbreviate("abcdefg", 6) == "abc..." &&
1414
abbreviate("abcdefg", 7) == "abcdefg" &&
1515
abbreviate("abcdefg", 8) == "abcdefg" &&
16-
abbreviate("abcdefg", 4) == "a..."
16+
abbreviate("abcdefg", 4) == "a..." &&
17+
concat("foo", "bar") == "foobar"
1718
then
1819
set_field("has_xyz", contains("abcdef", "xyz"));
1920
trigger_test();

0 commit comments

Comments
 (0)