I'm trying to write a fixed-width file using BeanIO library. Here's the record in question:
@Record
open class KeySegment(
@Field(at = 0, length = 1, required = true) var recordType: String = "",
@Field(at = 1, length = 6, required = true) var primaryCorpId: String = "",
@Field(at = 7, length = 16, minOccurs = 0) var creditCardAcc: String? = null,
@Field(at = 7, length = 8, minOccurs = 0) var companyId: String? = null,
@Field(at = 15, length = 8, minOccurs = 0) var sublevelId: String? = null,
@Field(at = 23, length = 8, required = true) var fileCreateDate: String = "",
@Field(at = 31, length = 8) var sourceId: String = "",
@Field(at = 39, length = 816) var filler: String = ""
)
Notice that creditCardAcc
and companyId + sublevelId
hold the same positions in the file. Depending on the use case, we either set creditCardAcc
field or companyId
and sublevelId
. Now for my use case, I want to set creditCardAcc
, but the problem is that companyId
and sublevelId
are padded with space and overwrite the creditCardAcc
field even if they are set to null
.
One solution is to pull those fields into two subclasses extending KeySegment
, and marshal subclass instead. But, I was wondering if there's a better native solution that I can use to accomplish this. For example, is there a way to disable the padding if the field is null?
Thanks.
There is no way to disable the padding when the field is null
. The padding
attribute has the following description here
If padding is enabled, the required field attribute has some control over the marshalling and unmarshalling of null values.
When unmarshalling a field consisting of all spaces in a fixed length stream, if required is false, the field is accepted regardless of the padding character. If required is true, a required field validation error is triggered. And when marshalling a null field value, if required is false, the field text is formatted as spaces regardless of the configured padding character.
The last sentence of the quote is exactly what you are asking about.