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:04 UTC

[camel] branch camel-3.20.x updated (7dd2ca69e0d -> e9b17d66a61)

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

davsclaus pushed a change to branch camel-3.20.x
in repository https://gitbox.apache.org/repos/asf/camel.git


    from 7dd2ca69e0d Bump version
     new 444b5af854c 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.
     new e9b17d66a61 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.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../camel/support/PropertyBindingSupportTest.java  | 24 ++++++++++++++++++++++
 .../camel/support/PropertyBindingSupport.java      | 16 +++++++++++++--
 .../modules/ROOT/pages/property-binding.adoc       |  6 ++++--
 3 files changed, 42 insertions(+), 4 deletions(-)


[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.

Posted by da...@apache.org.
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;


[camel] 02/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.

Posted by da...@apache.org.
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 e9b17d66a6179db59576c29c28293708ca6c36bb
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri May 26 15:14:58 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.
---
 docs/user-manual/modules/ROOT/pages/property-binding.adoc | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/docs/user-manual/modules/ROOT/pages/property-binding.adoc b/docs/user-manual/modules/ROOT/pages/property-binding.adoc
index 586c9ca4b8e..267e87a3fae 100644
--- a/docs/user-manual/modules/ROOT/pages/property-binding.adoc
+++ b/docs/user-manual/modules/ROOT/pages/property-binding.adoc
@@ -28,6 +28,8 @@ The `PropertyBindingSupport` class supports binding String valued properties to
        `#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)`.
+- _valueAs(type):value_ - 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.
 - _ignore case_ - Whether to ignore case for property keys (will ignore by default)
 
 == Property binding basics
@@ -43,7 +45,7 @@ For example to set brokers on the Kafka component you can do:
 camel.component.kafka.brokers = mykafka1,mykafka2
 ----
 
-This will essentially be equivavlent to configuring Kafka component in regular Java code via setters:
+This will essentially be equivalent to configuring Kafka component in regular Java code via setters:
 
 [source,java]
 ----
@@ -190,7 +192,7 @@ PropertyBindingSupport.build().withCamelContext(context).withTarget(foo)
 
 == More details
 
-Property binding is noteably used when running Camel in standalone mode with Camel Main, or using Camel Spring Boot, Camel K,
+Property binding is notably used when running Camel in standalone mode with Camel Main, or using Camel Spring Boot, Camel K,
 Camel Kafka Connector, or Camel Quarkus. All these runtimes have a similar way of configuring via property bindings such
 as from `application.properties` files.