diff --git a/pom.xml b/pom.xml index fc3111f..6e6c43b 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.tonysparks.jslt2 jslt2 - 0.2.4 + 0.2.5 A Virtual Machine based implementation of the JSLT json transformation language @@ -54,7 +54,7 @@ com.fasterxml.jackson.core jackson-databind - 2.9.8 + 2.9.10.1 @@ -68,7 +68,7 @@ com.schibsted.spt.data jslt - 0.1.5 + 0.1.8 test diff --git a/src/main/java/jslt2/Jslt2StdLibrary.java b/src/main/java/jslt2/Jslt2StdLibrary.java index 8c3d472..bc48742 100644 --- a/src/main/java/jslt2/Jslt2StdLibrary.java +++ b/src/main/java/jslt2/Jslt2StdLibrary.java @@ -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]; + } + }); diff --git a/src/main/java/jslt2/parser/Parser.java b/src/main/java/jslt2/parser/Parser.java index 3eb34c5..b9e425a 100644 --- a/src/main/java/jslt2/parser/Parser.java +++ b/src/main/java/jslt2/parser/Parser.java @@ -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; diff --git a/src/test/resources/query-tests.json b/src/test/resources/query-tests.json index 8ea0a97..7fc8096 100644 --- a/src/test/resources/query-tests.json +++ b/src/test/resources/query-tests.json @@ -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\"}" } - ] + ] } diff --git a/src/test/resources/simple-test.json b/src/test/resources/simple-test.json index c3d5f6d..7235fb4 100644 --- a/src/test/resources/simple-test.json +++ b/src/test/resources/simple-test.json @@ -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\"}" + } ] }