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

Added DelimitedFieldPaddingAttribute #373

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeremykdev
Copy link

Resolves #149 by added a DelimitedFieldPaddingAttribute

@mcavigelli mcavigelli added this to To do in Release 3.5 Nov 24, 2020
@mcavigelli mcavigelli moved this from To do to In progress in Release 3.5 Dec 19, 2020
Copy link
Collaborator

@mcavigelli mcavigelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there

Thank you for your pullrequest and the tests that show how your feature works.

  • Your attribute is named "Padding" but then the parameter is alignment. I find that confusing.
  • The logic is deeply integrated into FieldBase and DelimitedField. That will complicate maintenance of the library.
    -- most of the logic could be moved to a converter, which then could be configured, see https://www.filehelpers.net/example/Converters/CustomConverter/
    --- however with customconverters you cannot do the padding logic and conversion of types, for example DateTime formatting and then padding to a length.
    --- also parameters to the converter would not be type safe, they are just strings or object param arrays.

Matthias

var engine = new FileHelperEngine<AlignLeftSample>();
string output = engine.WriteString(records).TrimEnd();

string expected = "AA ,100 ,2020-01-01**";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the expectation should be the other way round.

The .Net string behaves as follows
var text = "AA".PadLeft(6);
// text is now " AA"

var engine = new FileHelperEngine<AlignCenterSample>();
string output = engine.WriteString(records).TrimEnd();

string expected = "++AA++,+100+,+2020-01-01+";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the value would be "AAA" and the field length would be 4:

" AAA" or "AAA " ?

var engine = new FileHelperEngine<AlignRightSample>();
string output = engine.WriteString(records).TrimEnd();

string expected = " AA,**100,++2020-01-01";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as left above: I think it should be the other way round.

@jeremykdev
Copy link
Author

jeremykdev commented Dec 23, 2020

Hi there

Thank you for your pullrequest and the tests that show how your feature works.

  • Your attribute is named "Padding" but then the parameter is alignment. I find that confusing.
  • The logic is deeply integrated into FieldBase and DelimitedField. That will complicate maintenance of the library.

Is there a way to avoid integrating the feature into the FieldBase and DelimitedField attributes and still allow for the converter to a) only apply to delimited fields and b) be compatible with also applying a converter such as for date conversions?

-- most of the logic could be moved to a converter, which then could be configured, see https://www.filehelpers.net/example/Converters/CustomConverter/

I avoided using a custom converted by design since the IMO the feature should allow a converter to run on the field and then apply padding. Also if a custom converter is used there could be confusion when applying padding to a fixed width field. IIRC fixed width field padding is supported by a separate attribute which is not compatible with delimited fields.

--- however with customconverters you cannot do the padding logic and conversion of types, for example DateTime formatting and then padding to a length.

That is why I opted to use a new attribute rather than a custom converter.

--- also parameters to the converter would not be type safe, they are just strings or object param arrays.

Matthias

To be clear I will be happy to re-write the code. I'm looking for feedback on how to approach it to best fit with the desired feature and style. Thanks for the feedback.

@mcavigelli
Copy link
Collaborator

Hi Jeremy

Still, I find a padding feature on a delimited feature confusing. And if we really need it, would it not be more intuitive to allow fixed fields on delimited records?

However I have simplified the extension with converters #380. It should significantly simplify the creation and the type safeness of custom converters. It eliminates the indirection of Enum.Custom to type and the attribute. Converters inherit from Attribute directly. This allows to create the converter directly on the field and hereby pass parameters to it in a type safe way.
This change brakes the 3.x API and can therefore only be included in the 4.x API.

Reuse of the contained built-in converters could further be simplified by making their methods virtual. A converting and padding custom-converter could be written by inheriting the converter (transformation) and calling the overriden method.

Another way to reuse the built-in converters without making its methods virtual would be a padding decorator. A converter would hold a reference to an other one, like double converter. After getting the double value, the padding decorator would pad the value.

@mcavigelli mcavigelli removed this from In progress in Release 3.5 Aug 22, 2022
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

Successfully merging this pull request may close these issues.

Support Padding via a Converted in the library
2 participants