Search in sources :

Example 6 with ParseException

use of io.atlasmap.expression.parser.ParseException in project atlasmap by atlasmap.

the class SELECT method create.

@Override
public Expression create(List<Expression> args) throws ParseException {
    if (args.size() != 2) {
        throw new ParseException("SELECT expects 2 arguments.");
    }
    Expression parentExpression = args.get(0);
    Expression selectExpression = args.get(1);
    return (ctx) -> {
        Field parent = parentExpression.evaluate(ctx);
        List<Field> collection = parent instanceof FieldGroup ? ((FieldGroup) parent).getField() : Arrays.asList(parent);
        List<Field> selected = new ArrayList<>();
        final FieldGroup answer = AtlasModelFactory.createFieldGroupFrom(parent, true);
        answer.setPath(AtlasModelFactory.GENERATED_PATH);
        for (Field f : collection) {
            Field fs = (Field) selectExpression.evaluate((subCtx) -> {
                if (subCtx != null && AtlasModelFactory.GENERATED_PATH.equals(answer.getPath())) {
                    answer.setPath(parent.getPath() + (subCtx.startsWith(AtlasPath.PATH_SEPARATOR) ? subCtx : (AtlasPath.PATH_SEPARATOR + subCtx)));
                }
                return AtlasPath.extractChildren(f, subCtx);
            });
            selected.add(fs);
        }
        if (selected.size() == 1) {
            return selected.get(0);
        }
        answer.getField().addAll(selected);
        return answer;
    };
}
Also used : AtlasModelFactory(io.atlasmap.v2.AtlasModelFactory) Arrays(java.util.Arrays) List(java.util.List) FieldGroup(io.atlasmap.v2.FieldGroup) Expression(io.atlasmap.expression.Expression) Field(io.atlasmap.v2.Field) AtlasPath(io.atlasmap.core.AtlasPath) BaseFunctionFactory(io.atlasmap.core.BaseFunctionFactory) ParseException(io.atlasmap.expression.parser.ParseException) ArrayList(java.util.ArrayList) Field(io.atlasmap.v2.Field) Expression(io.atlasmap.expression.Expression) FieldGroup(io.atlasmap.v2.FieldGroup) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(io.atlasmap.expression.parser.ParseException)

Aggregations

ParseException (io.atlasmap.expression.parser.ParseException)6 Expression (io.atlasmap.expression.Expression)5 Field (io.atlasmap.v2.Field)4 List (java.util.List)4 BaseFunctionFactory (io.atlasmap.core.BaseFunctionFactory)3 BooleanExpression (io.atlasmap.expression.internal.BooleanExpression)3 FieldGroup (io.atlasmap.v2.FieldGroup)3 AtlasPath (io.atlasmap.core.AtlasPath)2 AtlasModelFactory (io.atlasmap.v2.AtlasModelFactory)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 ExpressionContext (io.atlasmap.expression.ExpressionContext)1 FunctionResolver (io.atlasmap.expression.FunctionResolver)1 Parser (io.atlasmap.expression.parser.Parser)1 ActionProcessor (io.atlasmap.spi.ActionProcessor)1 FunctionFactory (io.atlasmap.spi.FunctionFactory)1 ActionParameter (io.atlasmap.v2.ActionParameter)1 ActionParameters (io.atlasmap.v2.ActionParameters)1 AtlasModelFactory.wrapWithField (io.atlasmap.v2.AtlasModelFactory.wrapWithField)1 StringReader (java.io.StringReader)1