Skip to content

Commit

Permalink
- fixes bug with usage of array index on quoted structure property (#369
Browse files Browse the repository at this point in the history
)

* fixes bug with usage of array index on quoted structure property
* documents how to use quoted names of structure elements, that contain spaces in their names
* disables local CI sonar checks, uses SonarCloud automatic analysis
* adds JaCoCo code coverage check to pom.xml and build.yml, removes sonar code coverage badge
  • Loading branch information
uklimaschewski authored Mar 25, 2023
1 parent 8f5159f commit 58406a9
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
distribution: 'corretto'
cache: maven
- name: Verify and analyze with SonarCloud
run: mvn --batch-mode verify sonar:sonar -Dsonar.projectKey=ezylang_EvalEx -Dsonar.organization=ezylang -Dsonar.host.url=https://sonarcloud.io -Dsonar.coverage.jacoco.xmlReportPaths=/home/runner/work/EvalEx/EvalEx/target/site/jacoco/jacoco.xml
## automatic analysis is enabled in SonarCloud, so it is disabled here
## run: mvn --batch-mode verify sonar:sonar -Dsonar.projectKey=ezylang_EvalEx -Dsonar.organization=ezylang -Dsonar.host.url=https://sonarcloud.io -Dsonar.coverage.jacoco.xmlReportPaths=/home/runner/work/EvalEx/EvalEx/target/site/jacoco/jacoco.xml
run: mvn --batch-mode clean verify
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ EvalEx - Java Expression Evaluator
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ezylang_EvalEx&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=ezylang_EvalEx)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=ezylang_EvalEx&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=ezylang_EvalEx)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=ezylang_EvalEx&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=ezylang_EvalEx)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=ezylang_EvalEx&metric=coverage)](https://sonarcloud.io/summary/new_code?id=ezylang_EvalEx)
[![Maven Central](https://img.shields.io/maven-central/v/com.ezylang/EvalEx.svg?label=Maven%20Central)](https://search.maven.org/search?q=a:%22EvalEx%22)

| :warning: Version 3 of EvalEx is a complete rewrite of the popular expression evaluator. See [the new documentation area](https://ezylang.github.io/EvalEx/concepts/changes.html) for an overview of the changes. |
Expand Down Expand Up @@ -174,7 +173,7 @@ big-math to EvalEx.

## Author and License

Copyright 2012-2022 by Udo Klimaschewski
Copyright 2012-2023 by Udo Klimaschewski

**Thanks to all who contributed to this
project: [Contributors](https://github.com/ezylang/EvalEx/graphs/contributors)**
Expand Down
29 changes: 29 additions & 0 deletions docs/concepts/datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,35 @@ BigDecimal result = expression.evaluate().getNumberValue();
System.out.println(result); // prints 44.85
```

#### Structure elements containing spaces in name
If your structure has element names that contain spaces, you can use double quotes in the expression to access them.

```java
Map<String, Object> data = new HashMap<>();
data.put("property 1", 12345);

Expression expression = new Expression("data.\"property 1\"")
.with("data", data);

BigDecimal result = expression.evaluate().getNumberValue();

System.out.println(result); // prints 12345
```

This also works with arrays.

```java
Map<String, Object> data = new HashMap<>();
data.put("property 1", Arrays.asList(1, 2, 3));

Expression expression = new Expression("data.\"property 1\"[1]")
.with("data", data);

BigDecimal result = expression.evaluate().getNumberValue();

System.out.println(result); // prints 2
```

### EXPRESSION_NODE

A string expression is converted into an abstract syntax tree (AST), which represents the expression
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ big-math to EvalEx.

## Author and License

Copyright 2012-2022 by Udo Klimaschewski
Copyright 2012-2023 by Udo Klimaschewski

**Thanks to all who contributed to this
project: [Contributors](https://github.com/ezylang/EvalEx/graphs/contributors)**
Expand Down
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,32 @@
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>check</id>
<phase>prepare-package</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>1.0</minimum>
</limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/ezylang/evalex/parser/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ private boolean arrayOpenOrStructureSeparatorNotAllowed() {
case BRACE_CLOSE:
case VARIABLE_OR_CONSTANT:
case ARRAY_CLOSE:
case STRING_LITERAL:
return false;
default:
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import com.ezylang.evalex.parser.ParseException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -106,4 +108,40 @@ void testThrowsFieldNotFound() {
.extracting("startPosition")
.isEqualTo(14);
}

@Test
void testStructureWithSpaceInName() throws EvaluationException, ParseException {
Map<String, BigDecimal> testStructure = new HashMap<>();
testStructure.put("field 1", new BigDecimal(88));

Expression expression = createExpression("a.\"field 1\"").with("a", testStructure);

assertThat(expression.evaluate().getStringValue()).isEqualTo("88");
}

@Test
void testTripleStructureWithSpaces() throws ParseException, EvaluationException {
Map<String, Object> structure = new HashMap<>();
Map<String, Object> subStructure = new HashMap<>();
subStructure.put("prop c", 99);
structure.put("prop b", List.of(subStructure));

Expression expression = createExpression("a.\"prop b\"[0].\"prop c\"").with("a", structure);

assertThat(expression.evaluate().getStringValue()).isEqualTo("99");
}

@Test
void testStructureWithSpaceInNameAndArrayAccess() throws EvaluationException, ParseException {
Map<String, List<Integer>> structure =
new HashMap<>() {
{
put("b prop", Arrays.asList(1, 2, 3));
}
};

Expression expression = createExpression("a.\"b prop\"[1]").with("a", structure);

assertThat(expression.evaluate().getStringValue()).isEqualTo("2");
}
}

0 comments on commit 58406a9

Please sign in to comment.