Skip to content

Commit

Permalink
Add replace() function to replace occurrences of a string (#4718)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschi authored and kmerz committed May 23, 2018
1 parent 31cf698 commit 636c94f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import org.graylog.plugins.pipelineprocessor.functions.strings.Lowercase;
import org.graylog.plugins.pipelineprocessor.functions.strings.RegexMatch;
import org.graylog.plugins.pipelineprocessor.functions.strings.RegexReplace;
import org.graylog.plugins.pipelineprocessor.functions.strings.Replace;
import org.graylog.plugins.pipelineprocessor.functions.strings.Split;
import org.graylog.plugins.pipelineprocessor.functions.strings.StartsWith;
import org.graylog.plugins.pipelineprocessor.functions.strings.Substring;
Expand Down Expand Up @@ -177,6 +178,7 @@ protected void configure() {
addMessageProcessorFunction(Join.NAME, Join.class);
addMessageProcessorFunction(Split.NAME, Split.class);
addMessageProcessorFunction(StartsWith.NAME, StartsWith.class);
addMessageProcessorFunction(Replace.NAME, Replace.class);

// json
addMessageProcessorFunction(JsonParse.NAME, JsonParse.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog.plugins.pipelineprocessor.functions.strings;

import com.google.common.primitives.Ints;
import org.apache.commons.lang3.StringUtils;
import org.graylog.plugins.pipelineprocessor.EvaluationContext;
import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction;
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs;
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;

import static com.google.common.collect.ImmutableList.of;

public class Replace extends AbstractFunction<String> {
public static final String NAME = "replace";
private final ParameterDescriptor<String, String> valueParam;
private final ParameterDescriptor<String, String> searchParam;
private final ParameterDescriptor<String, String> replacementParam;
private final ParameterDescriptor<Long, Integer> maxParam;

public Replace() {
valueParam = ParameterDescriptor.string("value").description("The text to search and replace in").build();
searchParam = ParameterDescriptor.string("search").description("The string to search for").build();
replacementParam = ParameterDescriptor.string("replacement").optional()
.description("The string to replace it with. Default: \"\"").build();
maxParam = ParameterDescriptor.integer("max", Integer.class).optional()
.transform(Ints::saturatedCast)
.description("Maximum number of occurrences to replace, or -1 if no maximum. Default: -1").build();
}

@Override
public String evaluate(FunctionArgs args, EvaluationContext context) {
final String text = valueParam.required(args, context);
final String searchString = searchParam.required(args, context);
final String replacement = replacementParam.optional(args, context).orElse("");
final int max = maxParam.optional(args, context).orElse(-1);

return StringUtils.replace(text, searchString, replacement, max);
}

@Override
public FunctionDescriptor<String> descriptor() {
return FunctionDescriptor.<String>builder()
.name(NAME)
.returnType(String.class)
.params(of(valueParam, searchParam, replacementParam, maxParam))
.description("Replaces the first \"max\" or all occurrences of a string within another string")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import org.graylog.plugins.pipelineprocessor.functions.strings.Lowercase;
import org.graylog.plugins.pipelineprocessor.functions.strings.RegexMatch;
import org.graylog.plugins.pipelineprocessor.functions.strings.RegexReplace;
import org.graylog.plugins.pipelineprocessor.functions.strings.Replace;
import org.graylog.plugins.pipelineprocessor.functions.strings.Split;
import org.graylog.plugins.pipelineprocessor.functions.strings.StartsWith;
import org.graylog.plugins.pipelineprocessor.functions.strings.Substring;
Expand Down Expand Up @@ -227,6 +228,7 @@ public static void registerFunctions() {
functions.put(Join.NAME, new Join());
functions.put(Split.NAME, new Split());
functions.put(StartsWith.NAME, new StartsWith());
functions.put(Replace.NAME, new Replace());

final ObjectMapper objectMapper = new ObjectMapperProvider().get();
functions.put(JsonParse.NAME, new JsonParse(objectMapper));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ when
ends_with("", "bar") == false &&
ends_with("foobar", "abc") == false &&
ends_with("foobar", "BAR") == false &&
ends_with("foobar", "BAR", true) == true
ends_with("foobar", "BAR", true) == true &&
replace("", "") == "" &&
replace("", "", "") == "" &&
replace("aabbcc", "a", "") == "bbcc" &&
replace("aabbcc", "", "zz") == "aabbcc" &&
replace("aaa", "a", "b", 1) == "baa" &&
replace("abba", "b", "k", 2) == "akka" &&
replace("abba", "ab", "ka") == "kaba" &&
// Example from https://github.com/Graylog2/graylog2-server/issues/4705
replace("#011#011#011RAISE 'my message'", "#011", "\t") == "\t\t\tRAISE 'my message'"
then
set_field("has_xyz", contains("abcdef", "xyz"));
set_field("string_literal", "abcd\\.e\tfg\u03a9\363");
Expand Down

0 comments on commit 636c94f

Please sign in to comment.