You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@metron.apache.org by nickwallen <gi...@git.apache.org> on 2016/12/03 15:54:13 UTC

[GitHub] incubator-metron pull request #370: METRON-586 add FILL_LEFT and FILL_RIGHT ...

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.  


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---