You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/05/26 13:17:05 UTC
[camel] 01/02: CAMEL-19393: Allow to specify if a value should be converter to a specific type when using property binding. This is needed with camel-kafka and other vendor integrations such as MS Azure.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.20.x
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 444b5af854c0add9f26ab71b22f87e43f28d18db
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri May 26 15:12:46 2023 +0200
CAMEL-19393: Allow to specify if a value should be converter to a specific type when using property binding. This is needed with camel-kafka and other vendor integrations such as MS Azure.
---
.../camel/support/PropertyBindingSupportTest.java | 24 ++++++++++++++++++++++
.../camel/support/PropertyBindingSupport.java | 16 +++++++++++++--
2 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
index e1e34e51472..3efcc9dffee 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
@@ -581,6 +581,30 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
}
}
+ @Test
+ public void testConvert() throws Exception {
+ Foo foo = new Foo();
+
+ Map<String, Object> prop = new HashMap<>();
+ prop.put("name", "James");
+ prop.put("bar.age", "#valueAs(Integer):33");
+ prop.put("bar.rider", "#valueAs(boolean):true");
+ prop.put("bar.gold-customer", "#valueAs(boolean):true");
+ prop.put("bar.work.id", "#valueAs(int):123");
+ prop.put("bar.work.name", "{{companyName}}");
+
+ PropertyBindingSupport.bindProperties(context, foo, prop);
+
+ assertEquals("James", foo.getName());
+ assertEquals(33, foo.getBar().getAge());
+ assertTrue(foo.getBar().isRider());
+ assertTrue(foo.getBar().isGoldCustomer());
+ assertEquals(123, foo.getBar().getWork().getId());
+ assertEquals("Acme", foo.getBar().getWork().getName());
+
+ assertTrue(prop.isEmpty(), "Should bind all properties");
+ }
+
public static class Foo {
private String name;
private Bar bar = new Bar();
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index 0af669f5a2a..9b9bb194838 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -71,6 +71,8 @@ import static org.apache.camel.util.StringHelper.startsWithIgnoreCase;
* #class:com.foo.MyClassType#myFactoryMethod('Hello World', 5, true). Or if you need to create the instance via
* constructor parameters then you can specify the parameters as shown: #class:com.foo.MyClass('Hello World', 5,
* true)</li>.
+ * <li>valueAs(type):value</li> - To declare that the value should be converted to the given type, such as
+ * #valueAs(int):123 which indicates that the value 123 should be converted to an integer.
* <li>ignore case - Whether to ignore case for property keys</li>
* </ul>
*
@@ -1212,12 +1214,13 @@ public final class PropertyBindingSupport {
}
// non reference parameters are
- // #bean: #class: #type: #property: #autowired
+ // #bean: #class: #type: #property: #convert: #autowired
if (parameter.equals("#autowired")
|| parameter.startsWith("#bean:")
|| parameter.startsWith("#class:")
|| parameter.startsWith("#type:")
- || parameter.startsWith("#property:")) {
+ || parameter.startsWith("#property:")
+ || parameter.startsWith("#valueAs(:")) {
return false;
}
@@ -1565,6 +1568,15 @@ public final class PropertyBindingSupport {
} else if (strval.startsWith("#bean:")) {
String key = strval.substring(6);
answer = camelContext.getRegistry().lookupByName(key);
+ } else if (strval.startsWith("#valueAs(")) {
+ String text = strval.substring(8);
+ String typeName = StringHelper.between(text, "(", ")");
+ String constant = StringHelper.after(text, ":");
+ if (typeName == null || constant == null) {
+ throw new IllegalArgumentException("Illegal syntax: " + text + " when using function #valueAs(type):value");
+ }
+ Class<?> type = camelContext.getClassResolver().resolveMandatoryClass(typeName);
+ answer = camelContext.getTypeConverter().mandatoryConvertTo(type, constant);
}
return answer;