Skip to content

Commit

Permalink
CAMEL-17331: add line number to model with camel-yaml-dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Dec 14, 2021
1 parent ab9f776 commit 1ca215a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Locale;

import org.apache.camel.LineNumberAware;
import org.apache.camel.util.StringHelper;
import org.snakeyaml.engine.v2.api.ConstructNode;
import org.snakeyaml.engine.v2.nodes.MappingNode;
Expand Down Expand Up @@ -47,12 +48,18 @@ public Object construct(Node node) {
} else if (node.getNodeType() == NodeType.MAPPING) {
MappingNode mn = (MappingNode) node;
target = newInstance();

setProperties(target, mn);
} else {
throw new IllegalArgumentException("Unsupported node type: " + node);
}

// enrich model with line number
if (target instanceof LineNumberAware && node.getStartMark().isPresent()) {
int line = node.getStartMark().get().getLine();
LineNumberAware lna = (LineNumberAware) target;
lna.setLineNumber(line);
}

return target;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@
@YamlProperty(name = "parameters", type = "object")
})
public class FromDefinitionDeserializer implements ConstructNode {

@Override
public Object construct(Node node) {
String uri = YamlSupport.creteEndpointUri(node, EndpointConsumerDeserializersResolver::resolveEndpointUri);
if (uri == null) {
throw new IllegalStateException("The endpoint URI must be set");
}
FromDefinition target = new FromDefinition(uri);

// enrich model with line number
if (node.getStartMark().isPresent()) {
int line = node.getStartMark().get().getLine();
target.setLineNumber(line);
}

return new FromDefinition(uri);
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ protected void setProperties(OutputAwareFromDefinition target, MappingNode node)
if (uri != null || properties != null) {
throw new IllegalArgumentException("uri and properties are not supported when using Endpoint DSL ");
}
target.setDelegate(new FromDefinition(endpointUri));
FromDefinition from = new FromDefinition(endpointUri);
// enrich model with line number
if (node.getStartMark().isPresent()) {
int line = node.getStartMark().get().getLine();
from.setLineNumber(line);
}
target.setDelegate(from);
} else {
throw new IllegalArgumentException("Unsupported field: " + key);
}
Expand All @@ -91,9 +97,13 @@ protected void setProperties(OutputAwareFromDefinition target, MappingNode node)

if (target.getDelegate() == null) {
ObjectHelper.notNull("uri", "The uri must set");

target.setDelegate(
new FromDefinition(YamlSupport.createEndpointUri(dc.getCamelContext(), uri, properties)));
FromDefinition from = new FromDefinition(YamlSupport.createEndpointUri(dc.getCamelContext(), uri, properties));
// enrich model with line number
if (node.getStartMark().isPresent()) {
int line = node.getStartMark().get().getLine();
from.setLineNumber(line);
}
target.setDelegate(from);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.
*/
package org.apache.camel.dsl.yaml

import org.apache.camel.dsl.yaml.support.YamlTestSupport
import org.apache.camel.model.FromDefinition
import org.apache.camel.model.LogDefinition
import org.apache.camel.model.RouteDefinition
import org.apache.camel.model.ToDefinition

class LineNumberTest extends YamlTestSupport {

def "line number definition"() {
when:
loadRoutes '''
- from:
uri: "direct:start"
steps:
- log:
logging-level: "ERROR"
message: "test"
log-name: "yaml"
- to: "direct:result"
'''
then:
context.routeDefinitions.size() == 1

with(context.routeDefinitions[0].input, FromDefinition) {
uri == "direct:start"
lineNumber == 2
}
with(context.routeDefinitions[0].outputs[0], LogDefinition) {
loggingLevel == 'ERROR'
message == 'test'
logName == 'yaml'
lineNumber == 5
}
with(context.routeDefinitions[0].outputs[1], ToDefinition) {
uri == "direct:result"
lineNumber == 8
}
}

def "line number route"() {
when:
loadRoutes '''
- route:
from:
uri: "direct:info"
steps:
- log: "message"
'''
then:
context.routeDefinitions.size() == 1

with(context.routeDefinitions[0], RouteDefinition) {
input.lineNumber == 3
input.endpointUri == 'direct:info'

with (outputs[0], LogDefinition) {
lineNumber == 5
message == 'message'
}
}
}

}

0 comments on commit 1ca215a

Please sign in to comment.