Skip to content

Commit

Permalink
Merge pull request #12 from ridencww/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ridencww authored Jun 1, 2018
2 parents 94af529 + d768e7f commit 2d4d852
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ Examples:

## Version History

2.2.7
* Fixed issue with tenary statements containing references to empty arrays throwing exceptions.

2.2.6
* Fixed offset for null parameters. Now, the first parameter is 1, not 0.
* Defer throwing ParseExceptions on tenary statements unless it occurred on the selected path.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.creativewidgetworks</groupId>
<artifactId>expression-evaluator</artifactId>
<version>2.2.6</version>
<version>2.2.7</version>

<packaging>jar</packaging>

Expand Down
38 changes: 21 additions & 17 deletions src/main/java/com/creativewidgetworks/expressionparser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -976,27 +976,31 @@ protected Value RPNtoValue(List<Token> tokens) throws ParserException {
if (index != null) {
List<Value> array = var.getValue().getArray();
int len = (array == null) ? 0 : array.size() - 1;
idx = index.getValue().asNumber().intValue();
if (idx < 0 || idx > len) {
setStatusAndFail(index, "error.index_out_of_range", String.valueOf(idx), String.valueOf(len));
}

val = new Value();
val.set(var.getValue().getArray().get(idx));
val.setName(valName);

if (subIndex != null) {
if (!ValueType.ARRAY.equals(val.getType())) {
setStatusAndFail(var, "error.expected_array", val.getType());
// Don't throw exceptions when processing tenaries
if (len >=0 || !suppressParseExceptions) {
idx = index.getValue().asNumber().intValue();
if (idx < 0 || idx > len) {
setStatusAndFail(index, "error.index_out_of_range", String.valueOf(idx), String.valueOf(len));
}

array = val.getArray();
len = (array == null) ? 0 : array.size() - 1;
idx = subIndex.getValue().asNumber().intValue();
if (idx < 0 || idx > len) {
setStatusAndFail(subIndex, "error.index_out_of_range", String.valueOf(idx), String.valueOf(len));
val = new Value();
val.set(var.getValue().getArray().get(idx));
val.setName(valName);

if (subIndex != null) {
if (!ValueType.ARRAY.equals(val.getType())) {
setStatusAndFail(var, "error.expected_array", val.getType());
}

array = val.getArray();
len = (array == null) ? 0 : array.size() - 1;
idx = subIndex.getValue().asNumber().intValue();
if (idx < 0 || idx > len) {
setStatusAndFail(subIndex, "error.index_out_of_range", String.valueOf(idx), String.valueOf(len));
}
val = val.getArray().get(idx);
}
val = val.getArray().get(idx);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ private void validateFieldParsed(String field) {
@Override
public Value getField(String name, boolean caseSensitive) {
lastFieldRequested = name;
return name.equalsIgnoreCase("not_found") ? null : new Value(name, name + ":123");

Value value = null;
if ("foo".equalsIgnoreCase(name)) {
value = new Value("foo", ValueType.ARRAY);
value.addValueToArray(null);
value.getArray().clear();
} else {
value = name.equalsIgnoreCase("not_found") ? null : new Value(name, name + ":123");
}

return value;
}

/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -77,6 +87,13 @@ public void testGetField_not_found() {
assertEquals("not_found", getLastFieldRequested());
}

@Test
public void testGetField_tenary_empty_array() {
FunctionToolbox.register(parser);
assertEquals("t", parser.eval("ARRAYLEN(@foo) == 0 ? 't' : @foo[0]").asString());
assertEquals("f", parser.eval("ARRAYLEN(@foo) != 0 ? @foo[0] : 'f'").asString());
}

@Test
public void testFieldExpressions() {
validateFieldParsed("@/name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public void testARRAYLEN() throws Exception {

validateNumericResult(parser, "ARRAYLEN(null)", null);
validateNumericResult(parser, "ARRAYLEN(V2)", "3");

validateNumericResult(parser, "ARRAYLEN(SPLIT(''))", "1");
validateNumericResult(parser, "ARRAYLEN(SPLIT('00,10,11,100'))", "4");

// No exceptions thrown on assignment when checking for null parameters
Expand Down

0 comments on commit 2d4d852

Please sign in to comment.