Skip to content

Commit e4375c8

Browse files
committed
feat(expressions): add built-in function 'or' and 'and' to ScEL
1 parent 28a6126 commit e4375c8

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

connect-file-pulse-expression/src/main/java/io/streamthoughts/kafka/connect/filepulse/expression/function/ExpressionFunctionExecutors.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import io.streamthoughts.kafka.connect.filepulse.expression.Expression;
2222
import io.streamthoughts.kafka.connect.filepulse.expression.ExpressionException;
23+
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.And;
2324
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Concat;
2425
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.ConcatWs;
2526
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Converts;
@@ -35,6 +36,7 @@
3536
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Matches;
3637
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Nlv;
3738
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Length;
39+
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Or;
3840
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.ReplaceAll;
3941
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.Split;
4042
import io.streamthoughts.kafka.connect.filepulse.expression.function.impl.StartsWith;
@@ -89,6 +91,8 @@ private ExpressionFunctionExecutors() {
8991
register(new Split());
9092
register(new UnixTimestamp());
9193
register(new If());
94+
register(new And());
95+
register(new Or());
9296
}
9397

9498
@SuppressWarnings("unchecked")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2021 StreamThoughts.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership.
7+
* The ASF licenses this file to You under the Apache License, Version 2.0
8+
* (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package io.streamthoughts.kafka.connect.filepulse.expression.function.impl;
20+
21+
import io.streamthoughts.kafka.connect.filepulse.data.TypedValue;
22+
import io.streamthoughts.kafka.connect.filepulse.expression.function.Arguments;
23+
import io.streamthoughts.kafka.connect.filepulse.expression.function.ExpressionFunction;
24+
import io.streamthoughts.kafka.connect.filepulse.expression.function.GenericArgument;
25+
26+
import java.util.stream.StreamSupport;
27+
28+
public class And implements ExpressionFunction {
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
@Override
34+
public TypedValue apply(final Arguments<GenericArgument> args) {
35+
final boolean result = StreamSupport
36+
.stream(args.spliterator(), false)
37+
.map(it -> (TypedValue) it.value())
38+
.filter(TypedValue::isNotNull)
39+
.allMatch(TypedValue::getBool);
40+
return TypedValue.bool(result);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2021 StreamThoughts.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership.
7+
* The ASF licenses this file to You under the Apache License, Version 2.0
8+
* (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package io.streamthoughts.kafka.connect.filepulse.expression.function.impl;
20+
21+
import io.streamthoughts.kafka.connect.filepulse.data.TypedValue;
22+
import io.streamthoughts.kafka.connect.filepulse.expression.function.Arguments;
23+
import io.streamthoughts.kafka.connect.filepulse.expression.function.ExpressionFunction;
24+
import io.streamthoughts.kafka.connect.filepulse.expression.function.GenericArgument;
25+
26+
import java.util.stream.StreamSupport;
27+
28+
public class Or implements ExpressionFunction {
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
@Override
34+
public TypedValue apply(final Arguments<GenericArgument> args) {
35+
final boolean result = StreamSupport
36+
.stream(args.spliterator(), false)
37+
.map(it -> (TypedValue) it.value())
38+
.filter(TypedValue::isNotNull)
39+
.anyMatch(TypedValue::getBool);
40+
41+
return TypedValue.bool(result);
42+
}
43+
}

connect-file-pulse-expression/src/test/java/io/streamthoughts/kafka/connect/filepulse/expression/function/impl/FunctionsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,20 @@ public void should_execute_if_function() {
218218
Assert.assertTrue(expressionTrue.readValue(context, TypedValue.class).value());
219219
Assert.assertFalse(expressionFalse.readValue(context, TypedValue.class).value());
220220
}
221+
222+
@Test
223+
public void should_execute_or_function() {
224+
Expression expressionTrue = parseExpression("{{ or(true, false) }}");
225+
Expression expressionFalse = parseExpression("{{ or(false, false) }}");
226+
Assert.assertTrue(expressionTrue.readValue(new StandardEvaluationContext(new Object()), TypedValue.class).value());
227+
Assert.assertFalse(expressionFalse.readValue(new StandardEvaluationContext(new Object()), TypedValue.class).value());
228+
}
229+
230+
@Test
231+
public void should_execute_and_function() {
232+
Expression expressionTrue = parseExpression("{{ and(true, true) }}");
233+
Expression expressionFalse = parseExpression("{{ and(true, false) }}");
234+
Assert.assertTrue(expressionTrue.readValue(new StandardEvaluationContext(new Object()), TypedValue.class).value());
235+
Assert.assertFalse(expressionFalse.readValue(new StandardEvaluationContext(new Object()), TypedValue.class).value());
236+
}
221237
}

0 commit comments

Comments
 (0)