Skip to content

Commit

Permalink
Merge pull request #18 from tonysparks/string-constant-bug
Browse files Browse the repository at this point in the history
fixed string index bug; added min/max per spec
  • Loading branch information
tonysparks committed Nov 12, 2019
2 parents b9d5734 + f851c67 commit ed7d601
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.tonysparks.jslt2</groupId>
<artifactId>jslt2</artifactId>
<version>0.2.4</version>
<version>0.2.5</version>

<description>
A Virtual Machine based implementation of the JSLT json transformation language
Expand Down Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
<version>2.9.10.1</version>
</dependency>

<!-- TESTS -->
Expand All @@ -68,7 +68,7 @@
<dependency>
<groupId>com.schibsted.spt.data</groupId>
<artifactId>jslt</artifactId>
<version>0.1.5</version>
<version>0.1.8</version>
<scope>test</scope>
</dependency>

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/jslt2/Jslt2StdLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ else if (arguments[0].isNull()) {
throw new Jslt2Exception("error: " + msg);
});

runtime.addFunction("min", 2, (input, arguments) -> {
// this works because null is the smallest of all values
if (Jslt2Util.compare(arguments[0], arguments[1]) < 0) {
return arguments[0];
}
else {
return arguments[1];
}
});

runtime.addFunction("max", 2, (input, arguments) -> {
if (arguments[0].isNull() || arguments[1].isNull()) {
return NullNode.instance;
}
else if (Jslt2Util.compare(arguments[0], arguments[1]) > 0) {
return arguments[0];
}
else {
return arguments[1];
}
});



Expand Down
8 changes: 4 additions & 4 deletions src/main/java/jslt2/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,15 @@ else if(match(DOT)) {
throw error(previous(), ErrorCode.UNEXPECTED_TOKEN);
}

Token name = null;
String identifier = null;
if(check(STRING)) {
name = consume(STRING, ErrorCode.MISSING_IDENTIFIER);
identifier = consume(STRING, ErrorCode.MISSING_IDENTIFIER).getValue().toString();
}
else {
name = consume(IDENTIFIER, ErrorCode.MISSING_IDENTIFIER);
identifier = consume(IDENTIFIER, ErrorCode.MISSING_IDENTIFIER).getText();
}

expr = node(new GetExpr(expr, name.getText()));
expr = node(new GetExpr(expr, identifier));
}
else {
break;
Expand Down
52 changes: 51 additions & 1 deletion src/test/resources/query-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,56 @@
"input" : "{ \"data\": { \"x\" : \"hello\" } }",
"query" : "{ \"y\": \"hello\"[1:3] }",
"output" : "{\"y\":\"el\"}"
},
{
"query": "min(1, 2)",
"input" : "null",
"output": "1"
},
{
"query": "min(2, 1)",
"input" : "null",
"output": "1"
},
{
"query": "min(\"a\", \"b\")",
"input" : "null",
"output": "\"a\""
},
{
"query": "min(null, 0)",
"input" : "null",
"output": "null"
},
{
"query": "max(1, 2)",
"input" : "null",
"output": "2"
},
{
"query": "max(2, 1)",
"input" : "null",
"output": "2"
},
{
"query": "max(\"a\", \"b\")",
"input" : "null",
"output": "\"b\""
},
{
"query": "max(null, 0)",
"input" : "null",
"output": "null"
},
{
"input" : "{ \"data\": { \"x\" : \"hello\" } }",
"query" : "{ \"y\": .data.\"x\" }",
"output" : "{\"y\":\"hello\"}"
},
{
"input" : "{ \"data\": { \"x\" : \"hello\" } }",
"query" : "{ \"y\": .data.x }",
"output" : "{\"y\":\"hello\"}"
}
]
]
}
10 changes: 5 additions & 5 deletions src/test/resources/simple-test.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"description" : "Simple one off test",
"tests" : [
{
"input" : "{ \"data\": { \"x\" : \"hello\" } }",
"query" : "{ \"y\": \"hello\"[1:3] }",
"output" : "{\"y\":\"el\"}"
}
{
"input" : "{ \"data\": { \"x\" : \"hello\" } }",
"query" : "{ \"y\": .data.\"x\" }",
"output" : "{\"y\":\"hello\"}"
}
]
}

0 comments on commit ed7d601

Please sign in to comment.