|
6 | 6 | import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
|
7 | 7 | import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
|
8 | 8 |
|
| 9 | +import java.util.Optional; |
| 10 | + |
9 | 11 | /**
|
10 | 12 | * This is the plugin. Your class should implement one of the existing plugin
|
11 | 13 | * interfaces. (i.e. AlarmCallback, MessageInput, MessageOutput)
|
12 | 14 | */
|
13 | 15 | public class Dec2HexFunction extends AbstractFunction<String> {
|
14 | 16 |
|
15 | 17 | public static final String NAME = "dec2hex";
|
16 |
| - private static final String PARAM = "long"; |
| 18 | + private static final String PARAM = "longval"; |
| 19 | + private static final String PARAM_LEN = "len"; |
| 20 | + |
| 21 | + private final ParameterDescriptor<Long, Long> valueParam = ParameterDescriptor |
| 22 | + .integer(PARAM) |
| 23 | + .description("A number, negative or positive.") |
| 24 | + .build(); |
| 25 | + |
| 26 | + private final ParameterDescriptor<Long, Long> lenParam = ParameterDescriptor |
| 27 | + .integer(PARAM_LEN) |
| 28 | + .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.") |
| 29 | + .build(); |
17 | 30 |
|
18 | 31 | @Override
|
19 | 32 | public String evaluate(FunctionArgs functionArgs, EvaluationContext evaluationContext) {
|
20 | 33 | Long number = valueParam.required(functionArgs, evaluationContext);
|
| 34 | + Optional<Long> numLength = lenParam.optional(functionArgs, evaluationContext); |
21 | 35 |
|
22 | 36 | if (number == null) return null;
|
23 | 37 |
|
24 |
| - return Long.toHexString(number); |
| 38 | + return String.format("%0" + String.valueOf(Math.abs(numLength.orElse(1L))) + "x", number); |
25 | 39 | }
|
26 | 40 |
|
27 |
| - private final ParameterDescriptor<Long, Long> valueParam = ParameterDescriptor |
28 |
| - .integer(PARAM) |
29 |
| - .description("A number, negative or positive.") |
30 |
| - .build(); |
31 |
| - |
32 | 41 | @Override
|
33 | 42 | public FunctionDescriptor<String> descriptor() {
|
34 | 43 | return FunctionDescriptor.<String>builder()
|
35 | 44 | .name(NAME)
|
36 |
| - .description("Returns hexadecimal lower case string representation of the given number. No prefix or leading zeros.") |
37 |
| - .params(valueParam) |
| 45 | + .description("Returns hexadecimal lower case string representation of the given number. No prefix, optionally left padded with zeros.") |
| 46 | + .params(valueParam, lenParam) |
38 | 47 | .returnType(String.class)
|
39 | 48 | .build();
|
40 | 49 | }
|
|
0 commit comments