Skip to content

Commit 58406a9

Browse files
- fixes bug with usage of array index on quoted structure property (#369)
* 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
1 parent 8f5159f commit 58406a9

File tree

7 files changed

+99
-4
lines changed

7 files changed

+99
-4
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ jobs:
2525
distribution: 'corretto'
2626
cache: maven
2727
- name: Verify and analyze with SonarCloud
28-
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
28+
## automatic analysis is enabled in SonarCloud, so it is disabled here
29+
## 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
30+
run: mvn --batch-mode clean verify
2931
env:
3032
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3133
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ EvalEx - Java Expression Evaluator
55
[![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)
66
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=ezylang_EvalEx&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=ezylang_EvalEx)
77
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=ezylang_EvalEx&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=ezylang_EvalEx)
8-
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=ezylang_EvalEx&metric=coverage)](https://sonarcloud.io/summary/new_code?id=ezylang_EvalEx)
98
[![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)
109

1110
| :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. |
@@ -174,7 +173,7 @@ big-math to EvalEx.
174173

175174
## Author and License
176175

177-
Copyright 2012-2022 by Udo Klimaschewski
176+
Copyright 2012-2023 by Udo Klimaschewski
178177

179178
**Thanks to all who contributed to this
180179
project: [Contributors](https://github.com/ezylang/EvalEx/graphs/contributors)**

docs/concepts/datatypes.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,35 @@ BigDecimal result = expression.evaluate().getNumberValue();
129129
System.out.println(result); // prints 44.85
130130
```
131131

132+
#### Structure elements containing spaces in name
133+
If your structure has element names that contain spaces, you can use double quotes in the expression to access them.
134+
135+
```java
136+
Map<String, Object> data = new HashMap<>();
137+
data.put("property 1", 12345);
138+
139+
Expression expression = new Expression("data.\"property 1\"")
140+
.with("data", data);
141+
142+
BigDecimal result = expression.evaluate().getNumberValue();
143+
144+
System.out.println(result); // prints 12345
145+
```
146+
147+
This also works with arrays.
148+
149+
```java
150+
Map<String, Object> data = new HashMap<>();
151+
data.put("property 1", Arrays.asList(1, 2, 3));
152+
153+
Expression expression = new Expression("data.\"property 1\"[1]")
154+
.with("data", data);
155+
156+
BigDecimal result = expression.evaluate().getNumberValue();
157+
158+
System.out.println(result); // prints 2
159+
```
160+
132161
### EXPRESSION_NODE
133162

134163
A string expression is converted into an abstract syntax tree (AST), which represents the expression

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ big-math to EvalEx.
163163

164164
## Author and License
165165

166-
Copyright 2012-2022 by Udo Klimaschewski
166+
Copyright 2012-2023 by Udo Klimaschewski
167167

168168
**Thanks to all who contributed to this
169169
project: [Contributors](https://github.com/ezylang/EvalEx/graphs/contributors)**

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,32 @@
156156
<goal>prepare-agent</goal>
157157
</goals>
158158
</execution>
159+
<execution>
160+
<id>check</id>
161+
<phase>prepare-package</phase>
162+
<goals>
163+
<goal>check</goal>
164+
</goals>
165+
<configuration>
166+
<rules>
167+
<rule>
168+
<element>BUNDLE</element>
169+
<limits>
170+
<limit implementation="org.jacoco.report.check.Limit">
171+
<counter>INSTRUCTION</counter>
172+
<value>COVEREDRATIO</value>
173+
<minimum>1.0</minimum>
174+
</limit>
175+
<limit implementation="org.jacoco.report.check.Limit">
176+
<counter>CLASS</counter>
177+
<value>MISSEDCOUNT</value>
178+
<maximum>0</maximum>
179+
</limit>
180+
</limits>
181+
</rule>
182+
</rules>
183+
</configuration>
184+
</execution>
159185
<execution>
160186
<id>report</id>
161187
<phase>prepare-package</phase>

src/main/java/com/ezylang/evalex/parser/Tokenizer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ private boolean arrayOpenOrStructureSeparatorNotAllowed() {
268268
case BRACE_CLOSE:
269269
case VARIABLE_OR_CONSTANT:
270270
case ARRAY_CLOSE:
271+
case STRING_LITERAL:
271272
return false;
272273
default:
273274
return true;

src/test/java/com/ezylang/evalex/ExpressionEvaluatorStructureTest.java

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

2121
import com.ezylang.evalex.parser.ParseException;
2222
import java.math.BigDecimal;
23+
import java.util.Arrays;
2324
import java.util.HashMap;
25+
import java.util.List;
2426
import java.util.Map;
2527
import org.junit.jupiter.api.Test;
2628

@@ -106,4 +108,40 @@ void testThrowsFieldNotFound() {
106108
.extracting("startPosition")
107109
.isEqualTo(14);
108110
}
111+
112+
@Test
113+
void testStructureWithSpaceInName() throws EvaluationException, ParseException {
114+
Map<String, BigDecimal> testStructure = new HashMap<>();
115+
testStructure.put("field 1", new BigDecimal(88));
116+
117+
Expression expression = createExpression("a.\"field 1\"").with("a", testStructure);
118+
119+
assertThat(expression.evaluate().getStringValue()).isEqualTo("88");
120+
}
121+
122+
@Test
123+
void testTripleStructureWithSpaces() throws ParseException, EvaluationException {
124+
Map<String, Object> structure = new HashMap<>();
125+
Map<String, Object> subStructure = new HashMap<>();
126+
subStructure.put("prop c", 99);
127+
structure.put("prop b", List.of(subStructure));
128+
129+
Expression expression = createExpression("a.\"prop b\"[0].\"prop c\"").with("a", structure);
130+
131+
assertThat(expression.evaluate().getStringValue()).isEqualTo("99");
132+
}
133+
134+
@Test
135+
void testStructureWithSpaceInNameAndArrayAccess() throws EvaluationException, ParseException {
136+
Map<String, List<Integer>> structure =
137+
new HashMap<>() {
138+
{
139+
put("b prop", Arrays.asList(1, 2, 3));
140+
}
141+
};
142+
143+
Expression expression = createExpression("a.\"b prop\"[1]").with("a", structure);
144+
145+
assertThat(expression.evaluate().getStringValue()).isEqualTo("2");
146+
}
109147
}

0 commit comments

Comments
 (0)