From 78dab343273e73b0c9581781bdd3d4199d29f57c Mon Sep 17 00:00:00 2001 From: Matt Mills Date: Thu, 27 May 2021 11:24:42 -0500 Subject: [PATCH] Enable fixed length field truncation to keep the end instead of the beginning --- .../Tests/Common/OverflowModeTests.cs | 28 ++++++++++++++++--- FileHelpers/Fields/FixedLengthField.cs | 4 ++- FileHelpers/Fields/OverflowMode.cs | 4 +++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/FileHelpers.Tests/Tests/Common/OverflowModeTests.cs b/FileHelpers.Tests/Tests/Common/OverflowModeTests.cs index d91074c23..3711aa957 100644 --- a/FileHelpers.Tests/Tests/Common/OverflowModeTests.cs +++ b/FileHelpers.Tests/Tests/Common/OverflowModeTests.cs @@ -9,12 +9,19 @@ namespace FileHelpers.Tests.CommonTests public class OverflowModeTests { [FixedLengthRecord] - public class DiscardCustomer + public class DiscardEndCustomer { [FieldFixedLength(10, OverflowMode = OverflowMode.DiscardEnd)] public string mCustomerID; } + [FixedLengthRecord] + public class DiscardStartCustomer + { + [FieldFixedLength(10, OverflowMode = OverflowMode.DiscardStart)] + public string mCustomerID; + } + [FixedLengthRecord] public class ErrorCustomer { @@ -26,10 +33,23 @@ public class ErrorCustomer [TestCase("0123456789A", "0123456789")] [TestCase("0123456789", "0123456789")] [TestCase("012345678", "012345678 ")] - public void Discard(string originalValue, string expectedValue) + public void DiscardEnd(string originalValue, string expectedValue) + { + var engine = new FixedFileEngine(); + var customer = new DiscardEndCustomer {mCustomerID = originalValue}; + var res = engine.WriteString(new[] {customer}); + + Check.That(res).IsEqualTo($"{expectedValue}{Environment.NewLine}"); + } + + [TestCase("0123456789ABC", "3456789ABC")] + [TestCase("0123456789A", "123456789A")] + [TestCase("0123456789", "0123456789")] + [TestCase("012345678", "012345678 ")] + public void DiscardStart(string originalValue, string expectedValue) { - var engine = new FixedFileEngine(); - var customer = new DiscardCustomer {mCustomerID = originalValue}; + var engine = new FixedFileEngine(); + var customer = new DiscardStartCustomer {mCustomerID = originalValue}; var res = engine.WriteString(new[] {customer}); Check.That(res).IsEqualTo($"{expectedValue}{Environment.NewLine}"); diff --git a/FileHelpers/Fields/FixedLengthField.cs b/FileHelpers/Fields/FixedLengthField.cs index 5d4ddd218..4c181d9e7 100644 --- a/FileHelpers/Fields/FixedLengthField.cs +++ b/FileHelpers/Fields/FixedLengthField.cs @@ -158,6 +158,8 @@ private string GetActualValueBasedOnFieldConfiguration(string field) throw new ConvertException(field, FieldType, $"Field value is too large for the field length ({FieldLength}) and field OverflowMode is set to {OverflowMode}."); + case OverflowMode.DiscardStart: + return field.Substring(field.Length - FieldLength); case OverflowMode.DiscardEnd: default: return field.Substring(0, FieldLength); @@ -186,4 +188,4 @@ protected override FieldBase CreateClone() #endregion } -} +} diff --git a/FileHelpers/Fields/OverflowMode.cs b/FileHelpers/Fields/OverflowMode.cs index 80f5cd9e9..1591fd57f 100644 --- a/FileHelpers/Fields/OverflowMode.cs +++ b/FileHelpers/Fields/OverflowMode.cs @@ -11,6 +11,10 @@ public enum OverflowMode /// DiscardEnd, /// + /// Discard overflowing characters at the start + /// + DiscardStart, + /// /// Throw an exception /// Error