0

I'm trying to read a flat file and do some processes. To do that I've defined a mapper. That mapper will assign the values for each property. In the document, the date will be represented with yyMMdd format and it can have "" or 000000 as a null value. That mean, if the date is 6 zeros or 6 blank spaces, the output should be null. I tried to do this by defining a NullFormater. But didn't work.

This is what I've tried:

============================

public class Test : DocumentRecordBase
{
public string StorageOrganisation { get; set; }
public Guid? StorageOrganisationId { get; set; }
public string StorageDescription { get; set; }
public DateTime? PaymentDueDate { get; set; }
public decimal? DiscountRate { get; set; }
public int? MaximumDaysDiscount { get; set; }
public DateTime? DateStorageChargeCommences { get; set; }
public decimal? StorageChargePerBalePerDay { get; set; }
public decimal? PenaltyInterestRate { get; set; }
public DateTime? LotAvailableDate { get; set; }
public decimal? PostSaleRechargeRebate { get; set; }

public Test() : base()
{
}

    public override T GetDocumentRecord<T>()
    {
        if (typeof(T) == typeof(Test))
        {
            return this as T;
        }
        return null;
    }


    public static IFixedLengthTypeMapper<Test> GetMapper()
    {
        var mapper = FixedLengthTypeMapper.Define<Test>();
        mapper.Property(r => r.RecordType, 2);
        mapper.Property(r => r.RecordSubType, 1);

        mapper.Property(r => r.PaymentDueDate, 6)
            .ColumnName("PaymentDueDate")
            .InputFormat("yyMMdd")
            .NullFormatter(NullFormatter.ForValue("000000")); // if the read value is "000000" or "      " then should pass as null


        mapper.CustomMapping(new RecordNumberColumn("RecordNumber")
        {
            IncludeSchema = true,
            IncludeSkippedRecords = true
        }, 0).WithReader(r => r.RecordNumber);
        return mapper;
    }

    public static bool GetMapperPredicate(string x)
    {
        return x.StartsWith("11A");
    }

}
Tân
  • 1
  • 15
  • 56
  • 102

1 Answers1

0

According to the definition of NullFormatter, (found here), you can only assign 1 fixed value. "If it is a fixed value, you can use the NullFormatter.ForValue method."

NullFormatter = NullFormatter.ForValue("NULL") 

If you use "000000", then it should convert 000000 to null otherwise, spaces will be considered actual values. Any number of 0s != 6 will result in non-null value as well.

Also, please define what you mean by "But didn't work". Please provide details and errors for elaboration

Jawad
  • 11,028
  • 3
  • 24
  • 37
  • Hi, Thank u so much for your help. I've referred the above link. But in my logic, I need to validate both null formatter types. Not only the '000000' – user2931283 Dec 03 '19 at 06:09
  • If you cant use .NullFormatter twice, not sure if it even allows multiple value conversion. ```.NullFormatter(NullFormatter.ForValue("000000")).NullFormatter(...); ``` – Jawad Dec 03 '19 at 13:17