[
https://issues.apache.org/jira/browse/METRON-586?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15718299#comment-15718299
]
ASF GitHub Bot commented on METRON-586:
---------------------------------------
Github user nickwallen commented on a diff in the pull request:
https://github.com/apache/incubator-metron/pull/370#discussion_r90759526
--- Diff:
metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/StringFunctions.java
---
@@ -222,4 +222,71 @@ public Object apply(List<Object> args) {
return null;
}
}
+
+ private enum FillDirection{
+ LEFT,
+ RIGHT
+ }
+
+ @Stellar(name="FILL_LEFT"
+ , description="Fills or pads a given string with a given
character, to a given length on the left"
+ , params = { "input - string", "fill - the fill character", "len
- the required length"}
+ , returns = "Filled String"
+ )
+ public static class FillLeft extends BaseStellarFunction {
+ @Override
+ public Object apply(List<Object> args) {
+ if(args.size() < 3) {
+ throw new IllegalStateException("FILL_LEFT expects three args:
[string,char,length] where char is the fill character string and length is the
required length of the result");
+ }
+ return fill(FillDirection.LEFT,args.get(0),args.get(1),args.get(2));
+ }
+ }
+
+ @Stellar(name="FILL_RIGHT"
+ , description="Fills or pads a given string with a given
character, to a given length on the right"
+ , params = { "input - string", "fill - the fill character", "len
- the required length"}
+ , returns = "Filled String"
+ )
+ public static class FillRight extends BaseStellarFunction {
+ @Override
+ public Object apply(List<Object> args) {
+ if(args.size() < 3) {
+ throw new IllegalStateException("FILL_RIGHT expects three args:
[string,char,length] where char is the fill character string and length is the
required length of the result");
+ }
+ return fill(FillDirection.RIGHT,args.get(0),args.get(1),args.get(2));
+ }
+ }
+
+ private static Object fill(FillDirection direction, Object inputObject,
Object fillObject, Object requiredLengthObject){
+ if(inputObject == null) {
+ return null;
+ }
+ String input = inputObject.toString();
+
+ if(requiredLengthObject == null || fillObject == null) {
+ return input;
--- End diff --
Should this blow-up and throw an exception if we don't have a length or
fill?
Consider the case where I write an expression for the length or fill. In
many cases, if something unexpected happens in that expression, like a missing
field, it returns null. So the null would propagate here and the original
string would be returned. I would wonder why I am just getting the original
string back when I expected it to be filled. That seems hard to debug.
> STELLAR should have FILL_LEFT and FILL_RIGHT functions
> ------------------------------------------------------
>
> Key: METRON-586
> URL: https://issues.apache.org/jira/browse/METRON-586
> Project: Metron
> Issue Type: Improvement
> Affects Versions: 0.3.0
> Reporter: Otto Fowler
>
> Stellar should support functions for filling string fields, left or right
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)