This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new e32bd84 CAMEL-15704: camel-csimple - Compiled simple language. e32bd84 is described below commit e32bd841809a29ca3206d2c3c3cb8d0b770693f0 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Nov 30 21:07:14 2020 +0100 CAMEL-15704: camel-csimple - Compiled simple language. --- .../language/csimple/joor/OriginalSimpleTest.java | 11 ++- .../simple/ast/SimpleFunctionExpression.java | 100 ++++++++++++++++++--- 2 files changed, 93 insertions(+), 18 deletions(-) diff --git a/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java index 4d586df..f72db88 100644 --- a/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java +++ b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java @@ -489,8 +489,11 @@ public class OriginalSimpleTest extends LanguageTestSupport { lines.add(new OrderLine(456, "ActiveMQ in Action")); exchange.setProperty("wicket", lines); - assertExpression("${exchangePropertyAs(wicket, List)[0].getId}", 123); - assertExpression("${exchangePropertyAs(wicket, List)[1].getName}", "ActiveMQ in Action"); + assertExpression("${exchangePropertyAsIndex(wicket, OrderLine, '0').getId}", 123); + assertExpression("${exchangePropertyAsIndex(wicket, OrderLine, '1').getName}", "ActiveMQ in Action"); + + assertExpression("${exchangePropertyAs(wicket, OrderLine)[0].getId}", 123); + assertExpression("${exchangePropertyAs(wicket, OrderLine)[1].getName}", "ActiveMQ in Action"); try { assertExpression("${exchangeProperty.wicket[2]}", ""); fail("Should have thrown an exception"); @@ -989,8 +992,8 @@ public class OriginalSimpleTest extends LanguageTestSupport { lines.add(new OrderLine(456, "ActiveMQ in Action")); exchange.getIn().setHeader("wicket", lines); - assertExpression("${header.wicket[0].getId}", 123); - assertExpression("${header.wicket[1].getName}", "ActiveMQ in Action"); + assertExpression("${headerAsIndex(wicket, OrderLine, 0).getId}", 123); + assertExpression("${headerAsIndex(wicket, OrderLine, 1).getName}", "ActiveMQ in Action"); try { assertExpression("${header.wicket[2]}", ""); fail("Should have thrown an exception"); diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java index becf312..00f9dcc 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java @@ -564,7 +564,11 @@ public class SimpleFunctionExpression extends LiteralExpression { } // body and headers first - answer = createCodeBodyOrHeader(function); + answer = createCodeBody(function); + if (answer != null) { + return answer; + } + answer = createCodeHeader(function); if (answer != null) { return answer; } @@ -863,7 +867,7 @@ public class SimpleFunctionExpression extends LiteralExpression { return null; } - private String createCodeBodyOrHeader(final String function) { + private String createCodeBody(final String function) { // bodyAsIndex String remainder = ifStartsWithReturnRemainder("bodyAsIndex(", function); if (remainder != null) { @@ -929,7 +933,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!last.isEmpty()) { func += "." + last; } - return createCodeBodyOrHeader(func); + return createCodeBody(func); } } return "bodyAs(message, " + type + ")" + ognlCodeMethods(remainder, type); @@ -1004,7 +1008,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!last.isEmpty()) { func += "." + last; } - return createCodeBodyOrHeader(func); + return createCodeBody(func); } } return "mandatoryBodyAs(message, " + type + ")" + ognlCodeMethods(remainder, type); @@ -1035,12 +1039,60 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!last.isEmpty()) { func += "." + last; } - return createCodeBodyOrHeader(func); + return createCodeBody(func); } } return "body" + ognlCodeMethods(remainder, null); } + return null; + } + + private String createCodeHeader(final String function) { + // headerAsIndex + String remainder = ifStartsWithReturnRemainder("headerAsIndex(", function); + if (remainder != null) { + String keyTypeAndIndex = StringHelper.before(remainder, ")"); + if (keyTypeAndIndex == null) { + throw new SimpleParserException( + "Valid syntax: ${headerAsIndex(key, type, index)} was: " + function, token.getIndex()); + } + String[] parts = keyTypeAndIndex.split(","); + if (parts.length != 3) { + throw new SimpleParserException( + "Valid syntax: ${headerAsIndex(key, type, index)} was: " + function, token.getIndex()); + } + String key = parts[0]; + String type = parts[1]; + String index = parts[2]; + if (ObjectHelper.isEmpty(key) || ObjectHelper.isEmpty(type) || ObjectHelper.isEmpty(index)) { + throw new SimpleParserException( + "Valid syntax: ${headerAsIndex(key, type, index)} was: " + function, token.getIndex()); + } + key = StringHelper.removeQuotes(key); + key = key.trim(); + type = StringHelper.removeQuotes(type); + if (!type.endsWith(".class")) { + type = type + ".class"; + } + type = type.replace('$', '.'); + type = type.trim(); + index = StringHelper.removeQuotes(index); + index = index.trim(); + remainder = StringHelper.after(remainder, ")"); + if (ObjectHelper.isNotEmpty(remainder)) { + boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder); + if (invalid) { + throw new SimpleParserException( + "Valid syntax: ${headerAsIndex(key, type, index).OGNL} was: " + function, token.getIndex()); + } + return "headerAsIndex(message, " + type + ", \"" + key + "\", \"" + index + "\")" + + ognlCodeMethods(remainder, type); + } else { + return "headerAsIndex(message, " + type + ", \"" + key + "\", \"" + index + "\")"; + } + } + // headerAs remainder = ifStartsWithReturnRemainder("headerAs(", function); if (remainder != null) { @@ -1071,6 +1123,8 @@ public class SimpleFunctionExpression extends LiteralExpression { return "message.getHeaders()"; } + // TODO: exchangePropertyAsIndex + // in header function remainder = ifStartsWithReturnRemainder("in.headers", function); if (remainder == null) { @@ -1100,19 +1154,37 @@ public class SimpleFunctionExpression extends LiteralExpression { if (invalid) { throw new SimpleParserException("Valid syntax: ${header.name[key]} was: " + function, token.getIndex()); } - // it is an index? - String index = null; - if (key.endsWith("]")) { - index = StringHelper.between(key, "[", "]"); - if (index != null) { - key = StringHelper.before(key, "["); + + // the key can contain index as it may be a map header.foo[0] + // and the key can also be OGNL (eg if there is a dot) + boolean index = false; + List<String> parts = splitOgnl(key); + if (parts.size() > 0) { + String s = parts.get(0); + int pos = s.indexOf('['); + if (pos != -1) { + index = true; + // split key into name and index + String before = s.substring(0, pos); + String after = s.substring(pos); + parts.set(0, before); + parts.add(1, after); } } - if (index != null) { - return "headerAsIndex(message, Object.class, \"" + key + "\", \"" + index + "\")"; + if (index) { + // is there any index, then we should use headerAsIndex function instead + // (use splitOgnl which assembles multiple indexes into a single part) + String func = "headerAsIndex(\"" + parts.get(0) + "\", Object.class, \"" + parts.get(1) + "\")"; + if (parts.size() > 2) { + String last = String.join("", parts.subList(2, parts.size())); + if (!last.isEmpty()) { + func += "." + last; + } + } + return createCodeHeader(func); } else if (OgnlHelper.isValidOgnlExpression(key)) { // ognl based header must be typed - throw new SimpleParserException("Valid syntax: ${headerAs(key, type)} was: " + function, token.getIndex()); + throw new SimpleParserException("Valid syntax: ${headerAs(key, type).OGNL} was: " + function, token.getIndex()); } else { // regular header return "header(message, \"" + key + "\")";