Skip to content

Commit

Permalink
bindy: added native support for methods registered with @Datafield
Browse files Browse the repository at this point in the history
  • Loading branch information
aldettinger committed Mar 2, 2021
1 parent 01f3001 commit 8aca58c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import org.apache.camel.dataformat.bindy.annotation.BindyConverter;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord;
import org.apache.camel.dataformat.bindy.annotation.FormatFactories;
import org.apache.camel.dataformat.bindy.annotation.Link;
Expand Down Expand Up @@ -118,5 +119,23 @@ void registerReflectiveClasses(CombinedIndexBuildItem index, BuildProducer<Refle
producer.produce(new ReflectiveClassBuildItem(false, false, t.name().toString()));
}
});

// Registering @DataField.method() classes for fields annotated with @DataField and using the method parameter
Stream.of(DataField.class)
.map(clazz -> DotName.createSimple(clazz.getName()))
.flatMap(dotName -> idx.getAnnotations(dotName).stream())
.filter(anno -> anno.value("method") != null && !anno.value("method").asString().isEmpty())
.filter(anno -> anno.target() != null && anno.target().kind() == Kind.FIELD)
.forEach(anno -> {
String method = anno.value("method").asString();
String methodClazz;
if (method.contains(".")) {
methodClazz = method.substring(0, method.lastIndexOf('.'));
} else {
methodClazz = anno.target().asField().type().toString();
}
LOG.debugf("Registering @DataField.method() class as reflective: %s", methodClazz);
producer.produce(new ReflectiveClassBuildItem(true, false, methodClazz));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class BindyResource {

private static final Logger LOG = Logger.getLogger(BindyResource.class);

private static final String CSV = "bindy-order-name-16,BINDY-COUNTRY";
private static final String CSV = "bindy-order-name-16,BINDY-COUNTRY,fr,A1B2C3";
private static final String FIXED_LENGTH_ORDER = "BobSpa\r\n";
private static final String MESSAGE_ORDER = "1=BE.CHM.0018=BEGIN9=2010=22022=458=camel - quarkus - bindy test\r\n";

Expand All @@ -60,6 +60,8 @@ public void marshalCsvRecordShouldSucceed() {
CsvOrder order = new CsvOrder();
order.setNameWithLengthSuffix(NameWithLengthSuffix.ofString("bindy-order-name"));
order.setCountry("bindy-country");
order.setLanguage("fr");
order.setClientReference("A1B2C3");

String marshalled = template.requestBody("direct:marshal-csv-record", order, String.class);

Expand All @@ -77,6 +79,8 @@ public void unMarshalCsvRecordShouldSucceed() {
assertNotNull(order.getNameWithLengthSuffix());
assertEquals("bindy-order-name-16-19", order.getNameWithLengthSuffix().toString());
assertEquals("B_ND_-C__NTR_", order.getCountry());
assertEquals("FR", order.getLanguage());
assertEquals("A_B_C_", order.getClientReference());
}

@Path("/marshalFixedLengthRecordShouldSucceed")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.quarkus.component.bindy.it.model;

public class CharReplacer {

public static String replaceDigitsWithUnderScore(String input) {
return input.replaceAll("([0-9])", "_");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public class CsvOrder {
@BindyConverter(TestConverter.class)
private String country;

@DataField(pos = 3, method = "toUpperCase")
private String language;

@DataField(pos = 4, method = "org.apache.camel.quarkus.component.bindy.it.model.CharReplacer.replaceDigitsWithUnderScore")
private String clientReference;

public NameWithLengthSuffix getNameWithLengthSuffix() {
return nameWithLengthSuffix;
}
Expand All @@ -47,4 +53,21 @@ public String getCountry() {
public void setCountry(String country) {
this.country = country;
}

public String getLanguage() {
return language;
}

public void setLanguage(String language) {
this.language = language;
}

public String getClientReference() {
return clientReference;
}

public void setClientReference(String clientReference) {
this.clientReference = clientReference;
}

}

0 comments on commit 8aca58c

Please sign in to comment.