Skip to content

Commit

Permalink
Merge #5441
Browse files Browse the repository at this point in the history
5441: [Backport 0.23] Dump feel-scala from 1.11.0 to 1.12.2 r=saig0 a=saig0

## Description

Backport of #5392

## Related issues

closes #5140
closes #4212


Co-authored-by: Philipp Ossler <philipp.ossler@gmail.com>
  • Loading branch information
zeebe-bors[bot] and saig0 committed Sep 28, 2020
2 parents 5344cbd + f917b39 commit 1c0f664
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
package io.zeebe.el;

import io.zeebe.el.impl.FeelExpressionLanguage;
import io.zeebe.util.sched.clock.ActorClock;

/** The entry point to create the default {@link ExpressionLanguage}. */
public class ExpressionLanguageFactory {

/** @return a new instance of the {@link ExpressionLanguage} */
public static ExpressionLanguage createExpressionLanguage() {
return new FeelExpressionLanguage();
return new FeelExpressionLanguage(ActorClock.current());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.zeebe.el.impl.feel.FeelToMessagePackTransformer;
import io.zeebe.el.impl.feel.FeelVariableContext;
import io.zeebe.el.impl.feel.MessagePackValueMapper;
import io.zeebe.util.sched.clock.ActorClock;
import java.util.regex.Pattern;
import org.camunda.feel.FeelEngine;
import org.camunda.feel.FeelEngine.Failure;
Expand All @@ -36,15 +37,20 @@ public final class FeelExpressionLanguage implements ExpressionLanguage {

private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\=(.+)", Pattern.DOTALL);

private final FeelEngine feelEngine =
new FeelEngine.Builder()
.customValueMapper(new MessagePackValueMapper())
.functionProvider(new FeelFunctionProvider())
.build();

private final FeelToMessagePackTransformer messagePackTransformer =
new FeelToMessagePackTransformer();

private final FeelEngine feelEngine;

public FeelExpressionLanguage(final ActorClock clock) {
feelEngine =
new FeelEngine.Builder()
.customValueMapper(new MessagePackValueMapper())
.functionProvider(new FeelFunctionProvider())
.clock(new ZeebeFeelEngineClock(clock))
.build();
}

@Override
public Expression parseExpression(final String expression) {
ensureNotNull("expression", expression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
* one or more contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright ownership.
* Licensed under the Zeebe Community License 1.0. You may not use this file
* except in compliance with the Zeebe Community License 1.0.
*/
package io.zeebe.el.impl;

import io.zeebe.util.sched.clock.ActorClock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.camunda.feel.FeelEngineClock;

public final class ZeebeFeelEngineClock implements FeelEngineClock {

private final ActorClock clock;

public ZeebeFeelEngineClock(final ActorClock clock) {
this.clock = clock;
}

@Override
public ZonedDateTime getCurrentTime() {

final long currentMillis = clock.getTimeMillis();
final var instant = Instant.ofEpochMilli(currentMillis);
final var zone = ZoneId.systemDefault();

return instant.atZone(zone);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import static io.zeebe.test.util.MsgPackUtil.asMsgPack;
import static org.assertj.core.api.Assertions.assertThat;

import io.zeebe.el.impl.FeelExpressionLanguage;
import io.zeebe.util.sched.clock.ControlledActorClock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import org.junit.Test;
Expand All @@ -18,8 +22,9 @@ public class FeelExpressionTest {

private static final EvaluationContext EMPTY_CONTEXT = name -> null;

private final ExpressionLanguage expressionLanguage =
ExpressionLanguageFactory.createExpressionLanguage();
private final ControlledActorClock clock = new ControlledActorClock();

private final ExpressionLanguage expressionLanguage = new FeelExpressionLanguage(clock);

@Test
public void stringLiteral() {
Expand Down Expand Up @@ -157,6 +162,72 @@ public void listProjection() {
assertThat(evaluationResult.getList()).isEqualTo(List.of(asMsgPack("1"), asMsgPack("2")));
}

@Test
public void getCurrentTime() {
final var localDateTime = LocalDateTime.parse("2020-09-21T07:20:00");
final var now = localDateTime.atZone(ZoneId.systemDefault());
clock.setCurrentTime(now.toInstant());

final var evaluationResult = evaluateExpression("now()", EMPTY_CONTEXT);

assertThat(evaluationResult.getType()).isEqualTo(ResultType.DATE_TIME);
assertThat(evaluationResult.getDateTime()).isEqualTo(now);
}

@Test
public void getCurrentDate() {
final var localDateTime = LocalDateTime.parse("2020-09-21T07:20:00");
final var now = localDateTime.atZone(ZoneId.systemDefault());
clock.setCurrentTime(now.toInstant());

final var evaluationResult = evaluateExpression("string(today())", EMPTY_CONTEXT);

assertThat(evaluationResult.getType()).isEqualTo(ResultType.STRING);
assertThat(evaluationResult.getString()).isEqualTo(now.toLocalDate().toString());
}

@Test
public void nullCheckWithNonExistingVariable() {
final var evaluationResult = evaluateExpression("x = null", EMPTY_CONTEXT);

assertThat(evaluationResult.getType()).isEqualTo(ResultType.BOOLEAN);
assertThat(evaluationResult.getBoolean()).isTrue();
}

@Test
public void nullCheckWithNestedNonExistingVariable() {
final var evaluationResult = evaluateExpression("x.y = null", EMPTY_CONTEXT);

assertThat(evaluationResult.getType()).isEqualTo(ResultType.BOOLEAN);
assertThat(evaluationResult.getBoolean()).isTrue();
}

@Test
public void checkIfDefinedWithNonExistingVariable() {
final var evaluationResult = evaluateExpression("is defined(x)", EMPTY_CONTEXT);

assertThat(evaluationResult.getType()).isEqualTo(ResultType.BOOLEAN);
assertThat(evaluationResult.getBoolean()).isFalse();
}

@Test
public void checkIfDefinedWithNestedNonExistingVariable() {
final var evaluationResult = evaluateExpression("is defined(x.y)", EMPTY_CONTEXT);

assertThat(evaluationResult.getType()).isEqualTo(ResultType.BOOLEAN);
assertThat(evaluationResult.getBoolean()).isFalse();
}

@Test
public void checkIfDefinedWithNullVariable() {
final var context = Map.of("x", asMsgPack("null"));

final var evaluationResult = evaluateExpression("is defined(x)", context::get);

assertThat(evaluationResult.getType()).isEqualTo(ResultType.BOOLEAN);
assertThat(evaluationResult.getBoolean()).isTrue();
}

private EvaluationResult evaluateExpression(
final String expression, final EvaluationContext context) {
final var parseExpression = expressionLanguage.parseExpression("=" + expression);
Expand Down
2 changes: 1 addition & 1 deletion parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<version.testcontainers>1.13.0</version.testcontainers>
<version.netflix.concurrency>0.3.6</version.netflix.concurrency>
<version.zeebe-test-container>0.32.0</version.zeebe-test-container>
<version.feel-scala>1.11.0</version.feel-scala>
<version.feel-scala>1.12.2</version.feel-scala>
<version.spring-framework>5.2.5.RELEASE</version.spring-framework>
<version.spring-boot>2.2.6.RELEASE</version.spring-boot>
<version.spring-boot>2.2.5.RELEASE</version.spring-boot>
Expand Down

0 comments on commit 1c0f664

Please sign in to comment.