Skip to content

Commit

Permalink
added second (optional) parameter to dec2hex function defining leadin…
Browse files Browse the repository at this point in the history
…g zero-padding length

Signed-off-by: Ľudovít Lučenič <ludovit.lucenic@digital-orchestra.sk>
  • Loading branch information
Ľudovít Lučenič committed Jan 4, 2017
1 parent 92c317c commit f6101f6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "HexDecFunctions",
"version": "1.0.0-SNAPSHOT",
"version": "1.1.0",
"description": "",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>sk.digitalorchestra.graylog.plugins</groupId>
<artifactId>graylog-plugin-hexdec-functions</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,44 @@
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;

import java.util.Optional;

/**
* This is the plugin. Your class should implement one of the existing plugin
* interfaces. (i.e. AlarmCallback, MessageInput, MessageOutput)
*/
public class Dec2HexFunction extends AbstractFunction<String> {

public static final String NAME = "dec2hex";
private static final String PARAM = "long";
private static final String PARAM = "longval";
private static final String PARAM_LEN = "len";

private final ParameterDescriptor<Long, Long> valueParam = ParameterDescriptor
.integer(PARAM)
.description("A number, negative or positive.")
.build();

private final ParameterDescriptor<Long, Long> lenParam = ParameterDescriptor
.integer(PARAM_LEN)
.description("Result string length. Result will be padded with leading zeroes to have at least len length. The sign of the parameter value is ignored. Defaults to 1.")
.build();

@Override
public String evaluate(FunctionArgs functionArgs, EvaluationContext evaluationContext) {
Long number = valueParam.required(functionArgs, evaluationContext);
Optional<Long> numLength = lenParam.optional(functionArgs, evaluationContext);

if (number == null) return null;

return Long.toHexString(number);
return String.format("%0" + String.valueOf(Math.abs(numLength.orElse(1L))) + "x", number);
}

private final ParameterDescriptor<Long, Long> valueParam = ParameterDescriptor
.integer(PARAM)
.description("A number, negative or positive.")
.build();

@Override
public FunctionDescriptor<String> descriptor() {
return FunctionDescriptor.<String>builder()
.name(NAME)
.description("Returns hexadecimal lower case string representation of the given number. No prefix or leading zeros.")
.params(valueParam)
.description("Returns hexadecimal lower case string representation of the given number. No prefix, optionally left padded with zeros.")
.params(valueParam, lenParam)
.returnType(String.class)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class Hex2DecFunction extends AbstractFunction<Long> {

public static final String NAME = "hex2dec";
private static final String PARAM = "string";
private static final String PARAM = "hexstring";

@Override
public Long evaluate(FunctionArgs functionArgs, EvaluationContext evaluationContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public URI getURL() {
@Override
public Version getVersion() {
return Version.fromPluginProperties(getClass(), PLUGIN_PROPERTIES, "version",
Version.from(1, 0, 0));
Version.from(1, 1, 0));
}

@Override
Expand Down

0 comments on commit f6101f6

Please sign in to comment.