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.