apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chinmaykolhatkar <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: APEXMALHAR-2010 Add Tranform o...
Date Mon, 21 Mar 2016 06:31:16 GMT
Github user chinmaykolhatkar commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/209#discussion_r56785718
  
    --- Diff: library/src/main/java/com/datatorrent/lib/util/PojoUtils.java ---
    @@ -696,11 +662,97 @@ private static Object createSetter(Class<?> pojoClass, String
setterExpr, String
           code = getSingleFieldSetterExpression(pojoClass, setterExpr, exprClass);
         }
     
    +    return compileExpression(code, setterClass, new String[] {PojoUtils.OBJECT, PojoUtils.VAL});
    +  }
    +
    +  /**
    +   * This method takes in expression, compiles the expression to provide a executable
form of expression.
    +   * This method uses {@link com.datatorrent.lib.expression.JavaExpressionParser} as
expression parser.
    +   *
    +   * @param inputType  Type of input object
    +   * @param expr       expression to be compiled.
    +   * @param returnType Return type of the expression.
    +   * @return Object of type {@link Expression} which can be directly executed.
    +   */
    +  public static Expression createExpression(Class<?> inputType, String expr, Class<?>
returnType)
    +  {
    +    return createExpression(inputType, expr, returnType, null);
    +  }
    +
    +  /**
    +   * This method takes in expression, compiles the expression to provide a executable
form of expression.
    +   * This methods also takes in list of classes and method which can be imported statically
in expression.
    +   * <p/>
    +   * This method uses {@link JavaExpressionParser} as expression parser.
    +   *
    +   * @param inputType      Type of input object
    +   * @param expr           expression to be compiled.
    +   * @param returnType     Return type of the expression.
    +   * @param defaultImports List of classes/method which will be statically imported to
expression compilation.
    +   * @return Object of type {@link Expression} which can be directly executed.
    +   */
    +  public static Expression createExpression(Class<?> inputType, String expr, Class<?>
returnType,
    +      String[] defaultImports)
    +  {
    +    JavaExpressionParser javaExpressionParser = new JavaExpressionParser();
    +    javaExpressionParser.setInputObjectPlaceholder("$", PojoUtils.OBJECT);
    +
    +    return createExpression(inputType, expr, returnType, defaultImports, javaExpressionParser);
    +  }
    +
    +  /**
    +   * This method takes in expression, compiles the expression to provide a executable
form of expression.
    +   * This methods also takes in list of classes and method which can be imported statically
in expression.
    +   * <p/>
    +   * Using this method one can override expression parser implementation.
    +   *
    +   * @param inputType      Type of input object
    +   * @param expr           expression to be compiled.
    +   * @param returnType     Return type of the expression.
    +   * @param defaultImports List of classes/method which will be statically imported to
expression compilation.
    +   * @param parser         Expression parser that should be used to parse expression.
    +   * @return Object of type {@link Expression} which can be directly executed.
    +   * @see {@link JavaExpressionParser} as a example.
    +   */
    +  public static Expression createExpression(Class<?> inputType, String expr, Class<?>
returnType,
    +      String[] defaultImports, Expression.ExpressionParser parser)
    +  {
    +    String code = parser.convertToCompilableExpression(expr, inputType, returnType);
    +
    +    return (Expression)compileExpression(code, Expression.class, new String[] {PojoUtils.OBJECT},
defaultImports);
    +  }
    +
    +  private static Object compileExpression(String code, Class<?> implClass, String[]
params)
    +  {
    +    return compileExpression(code, implClass, params, null);
    +  }
    +
    +  private static Object compileExpression(String code, Class<?> implClass, String[]
params, String[] defaultImports)
    +  {
    +    List<String> imports = new LinkedList<>();
    +    if (defaultImports != null && defaultImports.length != 0) {
    +      for (String defaultImport : defaultImports) {
    +        if (!defaultImport.startsWith("static")) {
    --- End diff --
    
    When adding a given import as static, that means the method will be directly available
in expression to be used. This way, the expression writer can use similar syntax like excel
functions.


---
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.
---

Mime
View raw message