Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiRecordEngine does not expose Options for all record types #429

Open
bcallaghan-et opened this issue Feb 15, 2023 · 1 comment
Open

Comments

@bcallaghan-et
Copy link

The MultiRecordEngine class exposes an Options property that lets developers dynamically access and change field properties, but that Options object only contains information about the first record type passed to the MultiRecordEngine's constructor. It is not possible to access the field options of the remaining types.

I have a record type that contains a variable length array. Unfortunately, this array is not the final field in the record. This record type also comes from a third-party, so I have no power to change it. A simplified version of this type is shown below.

[DelimitedRecord("~")]
internal class DetailReject {
    public string RecordType { get; set; }
    public int NumberOfErrorCodes { get; set; }
    public string[] ErrorCodes { get; set; }
    public string ReferenceNumber { get; set; }
}

If I try to use the [FieldArrayLength] attribute on the ErrorCodes property, it demands a single fixed value. The actual count of ErrorCodes is variable and included in the record itself as NumberOfErrorCodes.

I would like to dynamically change the ErrorCodes field options, similar to the Dynamic Engine Options example in the docs. However, I cannot access the options for this field because I'm using the MultiRecordEngine and the record type is not the first type passed to that constructor.

My workaround has been to add a dummy value to the [FieldArrayLength] attribute, and then to use reflection to access the internal and private fields that contain the field options for ErrorCodes. This logic is added to the BeforeRead method of the record type, which sets both ArrayMinLength and ArrayMaxLength to the actual count. FileHelpers then parses the record without issue.

Alternatively, a new API that allowed variable length arrays, with the expected length within the same record type, would be appreciated. Even in my workaround, I can't use the NumberOfErrorCodes property; I have to manually compute the number of error codes using only the RecordLine string. If possible, an attribute such as the following would meet my needs.

    public int NumberOfErrorCodes { get; set; }
    [FieldArrayLength(nameof(NumberOfErrorCodes))]
    public string[] ErrorCodes { get; set; }
@sandip4git
Copy link

Hi @bcallaghan-et ,
If you used array in the import model, it can parse array of any length coming from file.

Here is file content I tested

10248|VINET|5|04071996|01081996|16071996|3|32.38|["order1"|"order2"|"order3"]
10249|TOMSP|6|05071996|16081996|10071996|1|11.61|["order1"|"order2"]
ALFKI;Alfreds Futterkiste;Maria Anders;Sales Representative;Obere Str. 57;Berlin;Germany
ANATR;Ana Trujillo Emparedados y helados;Ana Trujillo;Owner;Avda. de la Constitución 2222;México D.F.;Mexico
10250|HANAR|4|08071996|05081996|12071996|2|65.83|["order1"]
10111314012345
11101314123456
10251|VICTE|3|08071996|05081996|15071996|1|41.34|["order4"]
11121314901234
10101314234567
ANTON;Antonio Moreno Taquería;Antonio Moreno;Owner;Mataderos 2312;México D.F.;Mexico
BERGS;Berglunds snabbköp;Christina Berglund;Order Administrator;Berguvsvägen 8;Luleå;Sweden

I used below import model

[DelimitedRecord(",")]
public class Order
{
public int OrderID;

 public string CustomerID;

 [FieldConverter(ConverterKind.Date, "ddMMyyyy")]
 public DateTime OrderDate;

 [FieldConverter(ConverterKind.Decimal, ".")] // The decimal separator is .
 public decimal Freight;

 **public string[] OrderItems;**

}

Code used to read file
var engine = new MultiRecordEngine(typeof(Orders),
typeof(Customer),
typeof(SampleType));

   CustomRecordTypeSelector recordTypeSelector = new CustomRecordTypeSelector();

   engine.RecordSelector = new RecordTypeSelector(recordTypeSelector.CustomSelector);

   var res = engine.ReadFile("..\\Files\\input.txt");

   foreach (var rec in res)
       Console.WriteLine(rec.ToString());

OutPut

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants