Skip to content

Commit

Permalink
feat(expressions): add built-in function 'or' and 'and' to ScEL
Browse files Browse the repository at this point in the history
  • Loading branch information
fhussonnois committed Sep 15, 2021
1 parent 28a6126 commit e4375c8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.streamthoughts.kafka.connect.filepulse.expression.Expression;
import io.streamthoughts.kafka.connect.filepulse.expression.ExpressionException;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.And;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Concat;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.ConcatWs;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Converts;
Expand All @@ -35,6 +36,7 @@
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Matches;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Nlv;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Length;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Or;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.ReplaceAll;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Split;
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.StartsWith;
Expand Down Expand Up @@ -89,6 +91,8 @@ private ExpressionFunctionExecutors() {
register(new Split());
register(new UnixTimestamp());
register(new If());
register(new And());
register(new Or());
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2021 StreamThoughts.
*
* 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 io.streamthoughts.kafka.connect.filepulse.expression.function.impl;

import io.streamthoughts.kafka.connect.filepulse.data.TypedValue;
import io.streamthoughts.kafka.connect.filepulse.expression.function.Arguments;
import io.streamthoughts.kafka.connect.filepulse.expression.function.ExpressionFunction;
import io.streamthoughts.kafka.connect.filepulse.expression.function.GenericArgument;

import java.util.stream.StreamSupport;

public class And implements ExpressionFunction {

/**
* {@inheritDoc}
*/
@Override
public TypedValue apply(final Arguments<GenericArgument> args) {
final boolean result = StreamSupport
.stream(args.spliterator(), false)
.map(it -> (TypedValue) it.value())
.filter(TypedValue::isNotNull)
.allMatch(TypedValue::getBool);
return TypedValue.bool(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2021 StreamThoughts.
*
* 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 io.streamthoughts.kafka.connect.filepulse.expression.function.impl;

import io.streamthoughts.kafka.connect.filepulse.data.TypedValue;
import io.streamthoughts.kafka.connect.filepulse.expression.function.Arguments;
import io.streamthoughts.kafka.connect.filepulse.expression.function.ExpressionFunction;
import io.streamthoughts.kafka.connect.filepulse.expression.function.GenericArgument;

import java.util.stream.StreamSupport;

public class Or implements ExpressionFunction {

/**
* {@inheritDoc}
*/
@Override
public TypedValue apply(final Arguments<GenericArgument> args) {
final boolean result = StreamSupport
.stream(args.spliterator(), false)
.map(it -> (TypedValue) it.value())
.filter(TypedValue::isNotNull)
.anyMatch(TypedValue::getBool);

return TypedValue.bool(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,20 @@ public void should_execute_if_function() {
Assert.assertTrue(expressionTrue.readValue(context, TypedValue.class).value());
Assert.assertFalse(expressionFalse.readValue(context, TypedValue.class).value());
}

@Test
public void should_execute_or_function() {
Expression expressionTrue = parseExpression("{{ or(true, false) }}");
Expression expressionFalse = parseExpression("{{ or(false, false) }}");
Assert.assertTrue(expressionTrue.readValue(new StandardEvaluationContext(new Object()), TypedValue.class).value());
Assert.assertFalse(expressionFalse.readValue(new StandardEvaluationContext(new Object()), TypedValue.class).value());
}

@Test
public void should_execute_and_function() {
Expression expressionTrue = parseExpression("{{ and(true, true) }}");
Expression expressionFalse = parseExpression("{{ and(true, false) }}");
Assert.assertTrue(expressionTrue.readValue(new StandardEvaluationContext(new Object()), TypedValue.class).value());
Assert.assertFalse(expressionFalse.readValue(new StandardEvaluationContext(new Object()), TypedValue.class).value());
}
}

0 comments on commit e4375c8

Please sign in to comment.