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 2021/12/08 12:47:31 UTC

[camel] branch main updated (a7f1d1f -> bb422d4)

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

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


    from a7f1d1f  CAMEL-17299: camel-jbang - Add --port option to enable embedded http server
     new 937264e  CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.
     new 50873d0  CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.
     new bb422d4  CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.

The 3 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:
 .../dsl/yaml/common/YamlDeserializerSupport.java   | 38 ++++++++++++++++++++++
 .../dsl/yaml/deserializers/ModelDeserializers.java | 18 +++++-----
 .../dsl/yaml/GenerateYamlDeserializersMojo.java    |  4 +--
 .../org/apache/camel/dsl/yaml/MarshalTest.groovy   | 11 ++++++-
 4 files changed, 59 insertions(+), 12 deletions(-)

[camel] 02/03: CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.

Posted by da...@apache.org.
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 50873d045f27db609aa41216b2549e8a4b95d239
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 8 13:44:03 2021 +0100

    CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.
---
 .../apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java
index 7d1ab38..9c490ef 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java
@@ -907,7 +907,7 @@ public class GenerateYamlDeserializersMojo extends GenerateYamlSupportMojo {
 
         ClassInfo c = view.getClassByName(field.type().name());
         if (c != null && c.isEnum()) {
-            cb.addStatement("target.set$L($L.valueOf(asText(node)))", StringHelper.capitalize(field.name()), field.type().name().toString());
+            cb.addStatement("target.set$L(asEnum(node, $L.class))", StringHelper.capitalize(field.name()), field.type().name().toString());
             cb.addStatement("break");
 
             Set<String> values = new TreeSet<>();
@@ -1050,7 +1050,7 @@ public class GenerateYamlDeserializersMojo extends GenerateYamlSupportMojo {
 
         ClassInfo c = view.getClassByName(parameterType.name());
         if (c != null && c.isEnum()) {
-            cb.addStatement("target.$L($L.valueOf(asText(node)))", method.name(), parameterType);
+            cb.addStatement("target.$L(asEnum(node, $L.class))", method.name(), parameterType);
             cb.addStatement("break");
 
             Set<String> values = new TreeSet<>();

[camel] 03/03: CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.

Posted by da...@apache.org.
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 bb422d45bf2783cffeaf9c7c45272fefa69e45cb
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 8 13:46:32 2021 +0100

    CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.
---
 .../dsl/yaml/deserializers/ModelDeserializers.java     | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
index 1804ce1..41b636b 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
@@ -679,7 +679,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "library": {
-                    target.setLibrary(org.apache.camel.model.dataformat.AvroLibrary.valueOf(asText(node)));
+                    target.setLibrary(asEnum(node, org.apache.camel.model.dataformat.AvroLibrary.class));
                     break;
                 }
                 case "module-class-names": {
@@ -7372,7 +7372,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "library": {
-                    target.setLibrary(org.apache.camel.model.dataformat.JsonLibrary.valueOf(asText(node)));
+                    target.setLibrary(asEnum(node, org.apache.camel.model.dataformat.JsonLibrary.class));
                     break;
                 }
                 case "module-class-names": {
@@ -10305,7 +10305,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "library": {
-                    target.setLibrary(org.apache.camel.model.dataformat.ProtobufLibrary.valueOf(asText(node)));
+                    target.setLibrary(asEnum(node, org.apache.camel.model.dataformat.ProtobufLibrary.class));
                     break;
                 }
                 case "module-class-names": {
@@ -11584,7 +11584,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "binding-mode": {
-                    target.setBindingMode(org.apache.camel.model.rest.RestBindingMode.valueOf(asText(node)));
+                    target.setBindingMode(asEnum(node, org.apache.camel.model.rest.RestBindingMode.class));
                     break;
                 }
                 case "client-request-validation": {
@@ -11638,7 +11638,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "host-name-resolver": {
-                    target.setHostNameResolver(org.apache.camel.model.rest.RestHostNameResolver.valueOf(asText(node)));
+                    target.setHostNameResolver(asEnum(node, org.apache.camel.model.rest.RestHostNameResolver.class));
                     break;
                 }
                 case "json-data-format": {
@@ -11946,7 +11946,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "collection-format": {
-                    target.setCollectionFormat(org.apache.camel.model.rest.CollectionFormat.valueOf(asText(node)));
+                    target.setCollectionFormat(asEnum(node, org.apache.camel.model.rest.CollectionFormat.class));
                     break;
                 }
                 case "data-format": {
@@ -11985,7 +11985,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "type": {
-                    target.setType(org.apache.camel.model.rest.RestParamType.valueOf(asText(node)));
+                    target.setType(asEnum(node, org.apache.camel.model.rest.RestParamType.class));
                     break;
                 }
                 default: {
@@ -12039,7 +12039,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "collection-format": {
-                    target.setCollectionFormat(org.apache.camel.model.rest.CollectionFormat.valueOf(asText(node)));
+                    target.setCollectionFormat(asEnum(node, org.apache.camel.model.rest.CollectionFormat.class));
                     break;
                 }
                 case "data-format": {
@@ -18333,7 +18333,7 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
                     break;
                 }
                 case "library": {
-                    target.setLibrary(org.apache.camel.model.dataformat.YAMLLibrary.valueOf(asText(node)));
+                    target.setLibrary(asEnum(node, org.apache.camel.model.dataformat.YAMLLibrary.class));
                     break;
                 }
                 case "max-aliases-for-collections": {

[camel] 01/03: CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.

Posted by da...@apache.org.
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 937264eb35ccf759a8a94bb9514f2da7887d392a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 8 13:39:51 2021 +0100

    CAMEL-17294: camel-yaml-dsl - Setting enum should work with mixed case and how camel-core does it.
---
 .../dsl/yaml/common/YamlDeserializerSupport.java   | 38 ++++++++++++++++++++++
 .../org/apache/camel/dsl/yaml/MarshalTest.groovy   | 11 ++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-common/src/main/java/org/apache/camel/dsl/yaml/common/YamlDeserializerSupport.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-common/src/main/java/org/apache/camel/dsl/yaml/common/YamlDeserializerSupport.java
index bceef93..dbd4c23 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl-common/src/main/java/org/apache/camel/dsl/yaml/common/YamlDeserializerSupport.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-common/src/main/java/org/apache/camel/dsl/yaml/common/YamlDeserializerSupport.java
@@ -155,6 +155,18 @@ public class YamlDeserializerSupport {
         return ((ScalarNode) node).getValue();
     }
 
+    public static <T> T asEnum(Node node, Class<T> type) throws YamlDeserializationException {
+        if (node == null) {
+            return null;
+        }
+        if (node.getNodeType() != NodeType.SCALAR) {
+            throw new IllegalArgumentException("Node is not SCALAR");
+        }
+
+        String text = ((ScalarNode) node).getValue();
+        return enumConverter(type, text);
+    }
+
     public static Map<String, Object> asMap(Node node) {
         if (node == null) {
             return null;
@@ -420,4 +432,30 @@ public class YamlDeserializerSupport {
 
         return null;
     }
+
+    public static <T> T enumConverter(Class<T> type, String value) {
+        if (type.isEnum()) {
+            String text = value.toString();
+            Class<Enum<?>> enumClass = (Class<Enum<?>>) type;
+
+            // we want to match case insensitive for enums
+            for (Enum<?> enumValue : enumClass.getEnumConstants()) {
+                if (enumValue.name().equalsIgnoreCase(text)) {
+                    return type.cast(enumValue);
+                }
+            }
+
+            // add support for using dash or camel cased to common used upper cased underscore style for enum constants
+            text = StringHelper.asEnumConstantValue(text);
+            for (Enum<?> enumValue : enumClass.getEnumConstants()) {
+                if (enumValue.name().equalsIgnoreCase(text)) {
+                    return type.cast(enumValue);
+                }
+            }
+
+            throw new IllegalArgumentException("Enum class " + type + " does not have any constant with value: " + text);
+        }
+
+        return null;
+    }
 }
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/MarshalTest.groovy b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/MarshalTest.groovy
index c664853..8356b8b 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/MarshalTest.groovy
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/MarshalTest.groovy
@@ -68,11 +68,20 @@ class MarshalTest extends YamlTestSupport {
                              data-format-type:
                                json: {}
                           - to: "mock:result"
+                    '''),
+                asResource('data-format-library-case', '''
+                    - from:
+                        uri: "direct:start"
+                        steps:    
+                          - marshal:
+                             json: 
+                               library: gson
+                          - to: "mock:result"
                     ''')
             ]
 
             expected << [
-                'json-gson', 'json-gson', 'json-jackson', 'json-jackson'
+                'json-gson', 'json-gson', 'json-jackson', 'json-jackson', 'json-gson'
             ]
     }
 }