[ 
https://issues.apache.org/jira/browse/CAMEL-9476?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15276874#comment-15276874
 ] 

Arno Noordover commented on CAMEL-9476:
---------------------------------------

Currently the paddingChar is only used for marshalling (exporting). I find it 
hard to decide what the most used use-case will be. Maybe we should try to 
create plugable marshalling and unmarshalling functionality and only provide 
one default implementation.
The hardest marshalling and unmarshalling in my opinion is fixed-width layout.
I think this should be the behaviour for unmarshalling:
* default-fillers (paddingChar) in a fixed-length field should be translated to 
a null object. The user didn't supply a value other than the default filler 
(paddingChar);
* padding characters should be removed from the correct side (left or right, 
based on the alignment). The user did supply a default filler on those 
position, i.e. no value;
* based on the previous two statements you might say that a record should be 
appended by the default filler (paddingChar).

But what would this mean for the following record:
We have two fields "foo", lenght 5, right aligned and "bar", length 5, left 
aligned.
The record definition defines a padding char '$' and a length of 10.
The record provided is "$foo".
Padding this gives "$foo$$$$$$". Unmarshalling this conforming the rules 
mentioned above means:
"foo" has a value of "foo$" (the left $ is trimmed because of the 
right-aligning.
"bar" is null because it only contains "$$$$$".

I would suggest the following for unmarshalling:
* Document the corner case when ignoring incorrect record lengths when padding 
is enabled;
* Pad with the padding character;
* trim the padding character from fields based on de alignment;
* if the trimmed field has a length of 0 the field should be set to null.

Please comment about what the use-case with the highest value will be.

> camel-bindy doesn't pad fixed length records
> --------------------------------------------
>
>                 Key: CAMEL-9476
>                 URL: https://issues.apache.org/jira/browse/CAMEL-9476
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-bindy
>    Affects Versions: 2.16.1
>            Reporter: Jostein Gogstad
>            Assignee: Luca Burgazzoli
>            Priority: Minor
>         Attachments: CAMEL_9476_Padding_and_trimming.patch
>
>
> Reading CAMEL-6039 one gets the impression that camel-bindy will pad fixed 
> length records if the input record is smaller than the fixed length.
> https://camel.apache.org/bindy.html:
> {quote}
> When the size of the data does not fill completely the length of the field, 
> we can then add 'padd' characters.
> {quote}
> This is not the case as the following test demonstrates. It fails with
> {code:none}
> java.lang.IllegalArgumentException: Size of the record: 5 is not equal to the 
> value provided in the model: 10
>       at 
> org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat.createModel(BindyFixedLengthDataFormat.java:248)
>       at 
> org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat.unmarshal(BindyFixedLengthDataFormat.java:209)
>       at 
> org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69)
> {code}
> {code:java}
> import org.apache.camel.EndpointInject;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.component.mock.MockEndpoint;
> import org.apache.camel.dataformat.bindy.annotation.DataField;
> import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord;
> import org.apache.camel.model.dataformat.BindyType;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.junit.Test;
> public class BindyTest extends CamelTestSupport {
>     public static final String URI_DIRECT_UNMARSHAL = "direct:unmarshall";
>     public static final String URI_MOCK_UNMARSHAL_RESULT = 
> "mock:unmarshal_result";
>     @EndpointInject(uri = URI_MOCK_UNMARSHAL_RESULT)
>     private MockEndpoint unmarhsalResult;
>     @Override
>     protected RouteBuilder createRouteBuilder() throws Exception {
>         return new RouteBuilder() {
>             @Override
>             public void configure() throws Exception {
>                 from(URI_DIRECT_UNMARSHAL)
>                     .unmarshal().bindy(BindyType.Fixed, MyBindyModel.class)
>                     .to(URI_MOCK_UNMARSHAL_RESULT);
>             }
>         };
>     }
>     @Test
>     public void testUnmarshal() throws Exception {
>         unmarhsalResult.expectedMessageCount(1);
>         template.sendBody(URI_DIRECT_UNMARSHAL, "foo  \r\n");
>         unmarhsalResult.assertIsSatisfied();
>         MyBindyModel myBindyModel = 
> unmarhsalResult.getReceivedExchanges().get(0).getIn().getBody(MyBindyModel.class);
>         assertEquals("foo  ", myBindyModel.foo);
>     }
>     @FixedLengthRecord(length = 10)
>     public class MyBindyModel {
>         @DataField(pos = 0, length = 5)
>         String foo;
>         @DataField(pos = 5, length = 5)
>         String bar;
>     }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to