This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit dcdd2343115c07a9916272ab54cf07877e4e38f5
Author: Claus Ibsen <claus.ib...@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 250c1ec6095..1c7293db519 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 6409e48aeb4..92145fad72a 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
@@ -72,6 +72,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>
  *
@@ -1247,12 +1249,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;
         }
 
@@ -1600,6 +1603,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;

Reply via email to