Skip to content

Commit

Permalink
MarcosMeli#149 Add Feature Padding for delimited records
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy committed Aug 15, 2018
1 parent ef80c42 commit 9d328c0
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 73 deletions.
77 changes: 77 additions & 0 deletions FileHelpers.Examples/Examples/18.Converters/70.PaddingConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using FileHelpers;

namespace ExamplesFx
{
//-> Name:Write Delimited File
//-> Description:Example of how to write a Delimited File
//-> AutoRun:true

public class PaddingConverterExample
: ExampleBase
{
//-> To write an output file (separated by a "|"):

//-> FileOut: Output.txt

// -> You use the same Record Mapping Class as you would to read it:
//-> File:RecordClass.cs
/// <summary>
/// Layout for a file delimited by |
/// </summary>
[DelimitedRecord("|")]
public class Orders
{
public int OrderID;

[FieldConverter(ConverterKind.Padding, 8, PaddingMode.Left, '0')]
public string CustomerID;

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

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

//-> /File

//-> Instantiate a FileHelperEngine and write the file:

public override void Run()
{
//-> File:Example.cs
var engine = new FileHelperEngine<Orders>();

var orders = new List<Orders>();

orders.Add(new Orders()
{
OrderID = 1,
CustomerID = "9001",
Freight = 82.43M,
OrderDate = new DateTime(2009, 05, 01)
});

orders.Add(new Orders()
{
OrderID = 2,
CustomerID = "9002",
Freight = 12.22M,
OrderDate = new DateTime(2009, 05, 02)
});

//engine now supports custom EOL for writing
engine.NewLineForWrite = "\n";

engine.WriteFile("Output.Txt", orders);

//-> /File

Console.WriteLine(engine.WriteString(orders));
}

//-> The classes you use could come from anywhere: LINQ to Entities, SQL database reads, or in this case, classes created within an application.
}
}
1 change: 1 addition & 0 deletions FileHelpers.Examples/FileHelpers.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="Examples\18.Converters\50.EnumConverter.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Examples\18.Converters\70.PaddingConverter.cs" />
<Compile Include="Examples\20.Events And Notification\05.INotifyRead.cs" />
<Compile Include="Examples\20.Events And Notification\10.INotifyWrite.cs" />
<Compile Include="Examples\25.ErrorHandling\10.ErrorMode.ThrowException.cs" />
Expand Down
170 changes: 99 additions & 71 deletions FileHelpers/Attributes/FieldConverterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,83 +37,32 @@ public FieldConverterAttribute(ConverterKind converter, string arg1, string arg2
public FieldConverterAttribute(ConverterKind converter, string arg1, string arg2, string arg3)
: this(converter, new string[] {arg1, arg2, arg3}) {}


/// <summary>
/// Indicates the <see cref="ConverterKind"/> used for read/write operations.
/// </summary>
/// <param name="converter">The <see cref="ConverterKind"/> used for the transformations.</param>
/// <param name="args">An array of parameters passed directly to the Converter</param>
private FieldConverterAttribute(ConverterKind converter, params string[] args)
{
public FieldConverterAttribute(ConverterKind converter, params object[] args)
{
Kind = converter;

Type convType;

switch (converter) {
case ConverterKind.Date:
convType = typeof (ConvertHelpers.DateTimeConverter);
break;

case ConverterKind.DateMultiFormat:
convType = typeof (ConvertHelpers.DateTimeMultiFormatConverter);
break;

case ConverterKind.Byte:
convType = typeof (ConvertHelpers.ByteConverter);
break;

case ConverterKind.SByte:
convType = typeof (ConvertHelpers.SByteConverter);
break;
Type convType = GetConverter(converter);
//mType = type;

case ConverterKind.Int16:
convType = typeof (ConvertHelpers.Int16Converter);
break;
case ConverterKind.Int32:
convType = typeof (ConvertHelpers.Int32Converter);
break;
case ConverterKind.Int64:
convType = typeof (ConvertHelpers.Int64Converter);
break;
CreateConverter(convType, args);
}

case ConverterKind.UInt16:
convType = typeof (ConvertHelpers.UInt16Converter);
break;
case ConverterKind.UInt32:
convType = typeof (ConvertHelpers.UInt32Converter);
break;
case ConverterKind.UInt64:
convType = typeof (ConvertHelpers.UInt64Converter);
break;

/// <summary>
/// Indicates the <see cref="ConverterKind"/> used for read/write operations.
/// </summary>
/// <param name="converter">The <see cref="ConverterKind"/> used for the transformations.</param>
/// <param name="args">An array of parameters passed directly to the Converter</param>
private FieldConverterAttribute(ConverterKind converter, params string[] args)
{
Kind = converter;

case ConverterKind.Decimal:
convType = typeof (ConvertHelpers.DecimalConverter);
break;
case ConverterKind.Double:
convType = typeof (ConvertHelpers.DoubleConverter);
break;
// Added by Shreyas Narasimhan 17 March 2010
case ConverterKind.PercentDouble:
convType = typeof (ConvertHelpers.PercentDoubleConverter);
break;
case ConverterKind.Single:
convType = typeof (ConvertHelpers.SingleConverter);
break;
case ConverterKind.Boolean:
convType = typeof (ConvertHelpers.BooleanConverter);
break;
// Added by Alexander Obolonkov 2007.11.08
case ConverterKind.Char:
convType = typeof (ConvertHelpers.CharConverter);
break;
// Added by Alexander Obolonkov 2007.11.08
case ConverterKind.Guid:
convType = typeof (ConvertHelpers.GuidConverter);
break;
default:
throw new BadUsageException("Converter '" + converter.ToString() +
"' not found, you must specify a valid converter.");
}
Type convType = GetConverter(converter);
//mType = type;

CreateConverter(convType, args);
Expand Down Expand Up @@ -207,12 +156,90 @@ private void CreateConverter(Type convType, object[] args)

else
throw new BadUsageException("The custom converter must inherit from ConverterBase");
}

}

#endregion

#region " Get ConverterType "
private Type GetConverter(ConverterKind converter)
{
Type convType;

#region " ArgsToTypes "
switch (converter)
{
case ConverterKind.Date:
convType = typeof(ConvertHelpers.DateTimeConverter);
break;

case ConverterKind.DateMultiFormat:
convType = typeof(ConvertHelpers.DateTimeMultiFormatConverter);
break;

case ConverterKind.Byte:
convType = typeof(ConvertHelpers.ByteConverter);
break;

case ConverterKind.SByte:
convType = typeof(ConvertHelpers.SByteConverter);
break;

case ConverterKind.Int16:
convType = typeof(ConvertHelpers.Int16Converter);
break;
case ConverterKind.Int32:
convType = typeof(ConvertHelpers.Int32Converter);
break;
case ConverterKind.Int64:
convType = typeof(ConvertHelpers.Int64Converter);
break;

case ConverterKind.UInt16:
convType = typeof(ConvertHelpers.UInt16Converter);
break;
case ConverterKind.UInt32:
convType = typeof(ConvertHelpers.UInt32Converter);
break;
case ConverterKind.UInt64:
convType = typeof(ConvertHelpers.UInt64Converter);
break;

case ConverterKind.Decimal:
convType = typeof(ConvertHelpers.DecimalConverter);
break;
case ConverterKind.Double:
convType = typeof(ConvertHelpers.DoubleConverter);
break;
// Added by Shreyas Narasimhan 17 March 2010
case ConverterKind.PercentDouble:
convType = typeof(ConvertHelpers.PercentDoubleConverter);
break;
case ConverterKind.Single:
convType = typeof(ConvertHelpers.SingleConverter);
break;
case ConverterKind.Boolean:
convType = typeof(ConvertHelpers.BooleanConverter);
break;
// Added by Alexander Obolonkov 2007.11.08
case ConverterKind.Char:
convType = typeof(ConvertHelpers.CharConverter);
break;
// Added by Alexander Obolonkov 2007.11.08
case ConverterKind.Guid:
convType = typeof(ConvertHelpers.GuidConverter);
break;
case ConverterKind.Padding:
convType = typeof(ConvertHelpers.PaddingConverter);
break;
default:
throw new BadUsageException("Converter '" + converter.ToString() +
"' not found, you must specify a valid converter.");
}
return convType;
}
#endregion

#region " ArgsToTypes "

private static Type[] ArgsToTypes(object[] args)
{
if (args == null) {
Expand Down Expand Up @@ -264,7 +291,8 @@ internal void ValidateTypes(FieldInfo fi)
fieldType = fieldType.GetGenericArguments()[0];

switch (Kind) {
case ConverterKind.None:
case ConverterKind.None:
case ConverterKind.Padding:
valid = true;
break;

Expand Down
94 changes: 93 additions & 1 deletion FileHelpers/Converters/ConvertHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ public override string FieldToString(object from)

#endregion

#region " GUID, Char, String Converters "
#region " GUID, Char, String, Padding Converters "

#region " Convert Classes "

Expand Down Expand Up @@ -1214,6 +1214,98 @@ public override string FieldToString(object from)
// }
//}

/// <summary>
/// Convert a numeric value with separators into a value
/// </summary>
internal sealed class PaddingConverter
: ConverterBase
{
/// <summary>
/// Total length of the field when written
/// </summary>
private readonly int totalLength;

/// <summary>
/// Padding to left or right
/// </summary>
private readonly PaddingMode paddingMode;

/// <summary>
/// Character to pad with
/// </summary>
private readonly Char paddingChar;

/// <summary>
/// Padding Converter with Length and option Padding Mode and Padding character
/// </summary>
/// <param name="TotalLength">Total length of the field when written</param>
/// <param name="PaddingMode">Whether to pad left or right</param>
/// /// <param name="PaddingCharacter">Character to pad with</param>
public PaddingConverter(Int32 TotalLength, PaddingMode PaddingMode = PaddingMode.Left, char PaddingCharacter = ' ')
{
totalLength = TotalLength;
paddingMode = PaddingMode;
paddingChar = PaddingCharacter;
}

/// <summary>
/// Convert a string to object for the record object
/// </summary>
/// <param name="from">String representation of the object</param>
/// <returns>object or empty</returns>
public override object StringToField(string from)
{
if (String.IsNullOrEmpty(from))
return string.Empty;

try
{
return from;
}
catch
{
throw new ConvertException(from, typeof(string));
}
}

/// <summary>
/// Convert a object to padded string
/// </summary>
/// <param name="from">Object to convert</param>
/// <returns>String converted to</returns>
public override string FieldToString(object from)
{
string paddedString = "";
try
{
paddedString = from.ToString();

if (paddedString.Length < totalLength)
{
int paddingLength = totalLength - paddedString.Length;
string strPadding = new string(paddingChar, paddingLength);
if (paddingMode == PaddingMode.Right)
{
return string.Concat(paddedString, strPadding);
}
else
{
return string.Concat(strPadding, paddedString);
}
}

return paddedString;
}
catch
{

throw new ConvertException(paddedString, from.GetType(), "Padding failed");
}

}

}

#endregion

#endregion
Expand Down
Loading

0 comments on commit 9d328c0

Please sign in to comment.