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 2022/01/06 16:20:57 UTC

[camel] branch main updated: camel-yaml-dsl - Fix line number if using comments, and from should use line number from uri if present

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


The following commit(s) were added to refs/heads/main by this push:
     new 2789c21  camel-yaml-dsl - Fix line number if using comments, and from should use line number from uri if present
2789c21 is described below

commit 2789c213e22f222a56136bce00ad062fb5863cac
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jan 6 17:20:13 2022 +0100

    camel-yaml-dsl - Fix line number if using comments, and from should use line number from uri if present
---
 .../deserializers/FromDefinitionDeserializer.java  | 18 ++++++++
 .../apache/camel/dsl/yaml/LineNumberTest.groovy    | 42 ++++++++++++++++++-
 .../src/test/resources/stuff/my-route-comment.yaml | 49 ++++++++++++++++++++++
 3 files changed, 107 insertions(+), 2 deletions(-)

diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/main/java/org/apache/camel/dsl/yaml/deserializers/FromDefinitionDeserializer.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/main/java/org/apache/camel/dsl/yaml/deserializers/FromDefinitionDeserializer.java
index 87a6ed8..b8e3ef0 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/main/java/org/apache/camel/dsl/yaml/deserializers/FromDefinitionDeserializer.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/main/java/org/apache/camel/dsl/yaml/deserializers/FromDefinitionDeserializer.java
@@ -24,8 +24,12 @@ import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.spi.annotations.YamlProperty;
 import org.apache.camel.spi.annotations.YamlType;
 import org.snakeyaml.engine.v2.api.ConstructNode;
+import org.snakeyaml.engine.v2.nodes.MappingNode;
 import org.snakeyaml.engine.v2.nodes.Node;
+import org.snakeyaml.engine.v2.nodes.NodeTuple;
+import org.snakeyaml.engine.v2.nodes.NodeType;
 
+import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.asText;
 import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.getDeserializationContext;
 
 @YamlType(
@@ -46,6 +50,20 @@ public class FromDefinitionDeserializer implements ConstructNode {
             line = node.getStartMark().get().getLine();
         }
 
+        // we want the line number of the "uri" when using from
+        if (node.getNodeType() == NodeType.MAPPING) {
+            final MappingNode mn = (MappingNode) node;
+            for (NodeTuple tuple : mn.getValue()) {
+                final String key = asText(tuple.getKeyNode());
+                if ("uri".equals(key)) {
+                    if (tuple.getKeyNode().getStartMark().isPresent()) {
+                        line = tuple.getKeyNode().getStartMark().get().getLine() + 1;
+                    }
+                    break;
+                }
+            }
+        }
+
         // from must be wrapped in a route, so use existing route or create a new route
         RouteDefinition route = (RouteDefinition) node.getProperty(RouteDefinition.class.getName());
         if (route == null) {
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/LineNumberTest.groovy b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/LineNumberTest.groovy
index 104aa55..10b02ae 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/LineNumberTest.groovy
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/LineNumberTest.groovy
@@ -69,7 +69,7 @@ class LineNumberTest extends YamlTestSupport {
         context.routeDefinitions.size() == 1
 
         with(context.routeDefinitions[0], RouteDefinition) {
-            input.lineNumber == 3
+            input.lineNumber == 4
             input.endpointUri == 'direct:info'
 
             with (outputs[0], LogDefinition) {
@@ -90,7 +90,7 @@ class LineNumberTest extends YamlTestSupport {
 
         with(context.routeDefinitions[0].input, FromDefinition) {
             uri == "quartz:foo?cron={{myCron}}"
-            lineNumber == 19
+            lineNumber == 20
         }
         with(context.routeDefinitions[0].outputs[0], LogDefinition) {
             message == 'Start'
@@ -117,4 +117,42 @@ class LineNumberTest extends YamlTestSupport {
         }
     }
 
+    def "line number file with comments"() {
+        setup:
+        def rloc = 'classpath:/stuff/my-route-comment.yaml'
+        def rdsl = context.resourceLoader.resolveResource(rloc)
+        when:
+        loadRoutes rdsl
+        then:
+        context.routeDefinitions.size() == 1
+
+        with(context.routeDefinitions[0].input, FromDefinition) {
+            uri == "quartz:foo?cron={{myCron}}"
+            lineNumber == 22
+        }
+        with(context.routeDefinitions[0].outputs[0], LogDefinition) {
+            message == 'Start'
+            lineNumber == 25
+        }
+        with(context.routeDefinitions[0].outputs[1], ToDefinition) {
+            uri == "bean:myBean?method=hello"
+            lineNumber == 26
+        }
+        with(context.routeDefinitions[0].outputs[3], ToDefinition) {
+            uri == "bean:myBean?method=bye"
+            lineNumber == 30
+        }
+        with(context.routeDefinitions[0].outputs[4], LogDefinition) {
+            message == '${body}'
+            lineNumber == 31
+        }
+        with(context.routeDefinitions[0].outputs[5], ChoiceDefinition) {
+            lineNumber == 33 // TODO: should be 32
+        }
+        with(context.routeDefinitions[0].outputs[6], LogDefinition) {
+            message == '${header.textProp}'
+            lineNumber == 49
+        }
+    }
+
 }
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/resources/stuff/my-route-comment.yaml b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/resources/stuff/my-route-comment.yaml
new file mode 100644
index 0000000..d7a65f0
--- /dev/null
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/resources/stuff/my-route-comment.yaml
@@ -0,0 +1,49 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+- route:
+    id: "foo"
+    from:
+      # some old code here
+      # and more old code here
+      uri: "quartz:foo?cron={{myCron}}"
+      steps:
+        # blah blah
+        - log: "Start"
+        - to: "bean:myBean?method=hello"
+        # more blah blah
+        # more blah blah
+        - log: "${body}"
+        - to: "bean:myBean?method=bye"
+        - log: "${body}"
+        - choice:
+            # more blah blah
+            when:
+              - simple: "${body} == 'Hello'"
+                steps:
+                  - set-property:
+                      # more blah blah
+                      name: testProp
+                      simple: "${body}"
+            # more blah blah
+            otherwise:
+              steps:
+                # more blah blah
+                - set-property:
+                    name: testProp
+                    constant: "NoDice"
+          # more blah blah
+        - log: "${header.textProp}"