Github user sandeepdeshmukh commented on a diff in the pull request:

    
https://github.com/apache/incubator-apex-malhar/pull/209#discussion_r56652885
  
    --- Diff: 
library/src/main/java/com/datatorrent/lib/transform/TransformOperator.java ---
    @@ -0,0 +1,172 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package com.datatorrent.lib.transform;
    +
    +import java.lang.reflect.Field;
    +import java.util.HashMap;
    +import java.util.LinkedList;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.commons.lang3.ClassUtils;
    +
    +import com.datatorrent.api.Context;
    +import com.datatorrent.api.DefaultInputPort;
    +import com.datatorrent.api.DefaultOutputPort;
    +import com.datatorrent.api.Operator;
    +import com.datatorrent.api.annotation.InputPortFieldAnnotation;
    +import com.datatorrent.api.annotation.OutputPortFieldAnnotation;
    +import com.datatorrent.common.util.BaseOperator;
    +import com.datatorrent.lib.expression.Expression;
    +import com.datatorrent.lib.util.PojoUtils;
    +
    +/**
    + * This operator can transform given POJO using provided expressions and
    + * return a final POJO as a return of transformation process.
    + *
    + * Following are the mandatory fields that needs to be set for 
TransformOperator to work:
    + * <ul>
    + *   <li><b>expressionMap</b> : Set how the transformation should 
happen</li>
    + *   <li><b>inputPort.attr.TUPLE_CLASS</b>: Set class type at input 
port</li>
    + *   <li><b>outputPort.attr.TUPLE_CLASS</b> : Set class type at output 
port</li>
    + * </ul>
    + *
    + * The operator uses interaction via {@link Expression} and {@link 
PojoUtils} to transform given POJO.
    + */
    +public class TransformOperator extends BaseOperator implements 
Operator.ActivationListener
    +{
    +  private Map<String, String> expressionMap = new HashMap<>();
    +  private List<String> expressionImports = new LinkedList<>();
    +
    +  private transient Map<PojoUtils.Setter, Expression> transformationMap = 
new HashMap<>();
    +  private Class<?> inputClass;
    +  private Class<?> outputClass;
    +
    +  public TransformOperator()
    +  {
    +    expressionImports.add("java.lang.Math.*");
    +    expressionImports.add("org.apache.commons.lang3.StringUtils.*");
    +  }
    +
    +  @InputPortFieldAnnotation(schemaRequired = true) public final transient 
DefaultInputPort<Object> input = new DefaultInputPort<Object>()
    +  {
    +    @Override public void setup(Context.PortContext context)
    +    {
    +      inputClass = context.getValue(Context.PortContext.TUPLE_CLASS);
    +    }
    +
    +    @Override public void process(Object o)
    +    {
    +      processTuple(o);
    +    }
    +  };
    +
    +  @OutputPortFieldAnnotation(schemaRequired = true) public final transient 
DefaultOutputPort<Object> output = new DefaultOutputPort<Object>()
    +  {
    +    @Override public void setup(Context.PortContext context)
    +    {
    +      outputClass = context.getValue(Context.PortContext.TUPLE_CLASS);
    +    }
    +  };
    +
    +  private void processTuple(Object in)
    +  {
    +    if (!inputClass.isAssignableFrom(in.getClass())) {
    +      throw new RuntimeException(
    +          "Unexpected tuple received. Received class: " + in.getClass() + 
". Expected class: " + inputClass.getClass());
    +    }
    +
    +    Object out;
    +    try {
    +      out = outputClass.newInstance();
    +    } catch (InstantiationException | IllegalAccessException e) {
    +      throw new RuntimeException("Failed to create new object", e);
    +    }
    +
    +    for (Map.Entry<PojoUtils.Setter, Expression> entry : 
transformationMap.entrySet()) {
    +      PojoUtils.Setter set = entry.getKey();
    +      Expression expr = entry.getValue();
    +      set.set(out, expr.execute(in));
    +    }
    --- End diff --
    
    Is there any default Identity function? 
    For example: If input POJO has a, b fields and output POJO has a,b,c and 
the expression is defined for  c = a+b.
    Will output.a=input.a and output.b=input.b be auto-set. This can always be 
overridden by say a = a-b.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to