use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class FILTER method create.
@Override
public Expression create(List<Expression> args) throws ParseException {
if (args.size() != 2) {
throw new ParseException("FILTER expects 2 arguments.");
}
Expression parentExpression = args.get(0);
BooleanExpression filterExpression = BooleanExpression.asBooleanExpression(args.get(1));
return (ctx) -> {
Field parent = (Field) parentExpression.evaluate(ctx);
List<Field> collection = parent instanceof FieldGroup ? ((FieldGroup) parent).getField() : Arrays.asList(parent);
FieldGroup filtered = AtlasModelFactory.createFieldGroupFrom(parent, true);
int index = 0;
for (Field f : collection) {
if (filterExpression.matches((subCtx) -> {
return AtlasPath.extractChildren(f, subCtx);
})) {
adjustRootCollectionIndex(f, index);
index++;
filtered.getField().add(f);
}
}
return filtered;
};
}
use of io.atlasmap.v2.Expression 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;
};
}
use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class ExpressionFieldActionTest method testNull.
@Test
public void testNull() throws Exception {
Expression action = new Expression();
action.setExpression("IF(${0} == null, 'null', 'not null')");
assertEquals("null", ExpressionFieldAction.process(action, Arrays.asList((Object) null)));
assertEquals("not null", ExpressionFieldAction.process(action, Arrays.asList("")));
}
use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class ExpressionFieldActionTest method testAdd.
/*
* operators
*/
@Test
public void testAdd() throws Exception {
Expression action = new Expression();
action.setExpression("${0} + ${1}");
assertEquals(110, ExpressionFieldAction.process(action, Arrays.asList(10, 100)));
assertEquals("foobar", ExpressionFieldAction.process(action, Arrays.asList("foo", "bar")));
}
use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class ExpressionFieldActionTest method testAddNestedAction.
@Test
public void testAddNestedAction() throws Exception {
Expression action = new Expression();
action.setExpression("IF(ADD(${0}, ${1}, ${2}) == ${3}, 'pass', 'fail')");
assertEquals("pass", ExpressionFieldAction.process(action, Arrays.asList(1, 2, 3, 6)));
}
Aggregations