Skip to content

Commit

Permalink
Changed delegate ShouldQuote to include the field type.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshClose committed Jan 29, 2021
1 parent 4087082 commit ceaa383
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 17 deletions.
2 changes: 1 addition & 1 deletion performance/CsvHelper.Performance/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void WriteField(int columns = 50, int rows = 2_000_000, bool quoteAllFiel
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
//Delimiter = ";",
ShouldQuote = (field, context) => quoteAllFields,
ShouldQuote = (_, _, _) => quoteAllFields,
};
using (var stream = File.Create(GetFilePath()))
using (var writer = new StreamWriter(stream))
Expand Down
2 changes: 1 addition & 1 deletion src/CsvHelper/Configuration/ConfigurationFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static bool ReadingExceptionOccurred(CsvHelperException exception)
/// <param name="field">The current field.</param>
/// <param name="row">The current row.</param>
/// <returns></returns>
public static bool ShouldQuote(string field, IWriterRow row)
public static bool ShouldQuote(string field, Type fieldType, IWriterRow row)
{
var config = row.Configuration;

Expand Down
9 changes: 6 additions & 3 deletions src/CsvHelper/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public class CsvWriter : IWriter

private bool disposed;
private bool hasHeaderBeenWritten;
private bool hasRecordBeenWritten;
private int row = 1;
private int index;
private char[] buffer;
private int bufferSize;
private int bufferPosition;
private Type fieldType;

/// <inheritdoc/>
public virtual string[] HeaderRecord { get; private set; }
Expand Down Expand Up @@ -145,7 +145,9 @@ public virtual void WriteField(string field)
field = field.Trim();
}

var shouldQuoteResult = shouldQuote(field, this);
fieldType ??= typeof(string);

var shouldQuoteResult = shouldQuote(field, fieldType, this);

WriteField(field, shouldQuoteResult);
}
Expand Down Expand Up @@ -182,6 +184,7 @@ public virtual void WriteField(string field, bool shouldQuote)

CopyToBuffer(field);
index++;
fieldType = null;
}

/// <inheritdoc/>
Expand All @@ -195,7 +198,7 @@ public virtual void WriteField<T>(T field)
/// <inheritdoc/>
public virtual void WriteField<T>(T field, ITypeConverter converter)
{
var type = field == null ? typeof(string) : field.GetType();
var type = fieldType = field == null ? typeof(string) : field.GetType();
reusableMemberMapData.TypeConverter = converter;
if (!typeConverterOptionsCache.TryGetValue(type, out TypeConverterOptions typeConverterOptions))
{
Expand Down
2 changes: 1 addition & 1 deletion src/CsvHelper/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace CsvHelper
/// <summary>
/// Function that is used to determine if a field should get quoted when writing.
/// </summary>
public delegate bool ShouldQuote(string field, IWriterRow row);
public delegate bool ShouldQuote(string field, Type fieldType, IWriterRow row);

/// <summary>
/// Function that determines whether to skip the given record or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void Constructor_AllArguments_SetsProperites()
PrepareHeaderForMatch prepareHeaderForMatch = (header, fieldIndex) => header;
ReadingExceptionOccurred readingExceptionOccurred = (ex) => true;
ReferenceHeaderPrefix referenceHeaderPrefix = (type, memberName) => string.Empty;
ShouldQuote shouldQuote = (field, row) => true;
ShouldQuote shouldQuote = (_, _, _) => true;
ShouldSkipRecord shouldSkipRecord = (record) => true;
ShouldUseConstructorParameters shouldUseConstructorParameters = (parameterType) => true;

Expand Down
8 changes: 4 additions & 4 deletions tests/CsvHelper.Tests/CsvWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void WriteRecordsAllFieldsQuotedTest()

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldQuote = (field, context) => true,
ShouldQuote = (_, _, _) => true,
};

string csv;
Expand Down Expand Up @@ -352,7 +352,7 @@ public void WriteRecordsNoFieldsQuotedTest()

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldQuote = (field, context) => false,
ShouldQuote = (_, _, _) => false,
};
string csv;
using (var stream = new MemoryStream())
Expand Down Expand Up @@ -449,7 +449,7 @@ public void WriteRecordWithQuoteAllFieldsOnAndDelimiterInFieldTest()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldQuote = (field, context) => true,
ShouldQuote = (_, _, _) => true,
};
using (var stream = new MemoryStream())
using (var reader = new StreamReader(stream))
Expand All @@ -476,7 +476,7 @@ public void WriteRecordWithQuoteAllFieldsOnAndQuoteInFieldTest()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldQuote = (field, context) => true,
ShouldQuote = (_, _, _) => true,
};
using (var stream = new MemoryStream())
using (var reader = new StreamReader(stream))
Expand Down
42 changes: 42 additions & 0 deletions tests/CsvHelper.Tests/Writing/FieldTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using CsvHelper.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsvHelper.Tests.Writing
{
[TestClass]
public class FieldTypeTests
{
[TestMethod]
public void Test1()
{
Type type = null;
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldQuote = (field, fieldType, row) =>
{
type = fieldType;
return ConfigurationFunctions.ShouldQuote(field, fieldType, row);
},
};
using (var writer = new StringWriter())
using (var csv = new CsvWriter(writer, config))
{
csv.WriteField(string.Empty);
Assert.AreEqual(typeof(string), type);

csv.WriteField(1);
Assert.AreEqual(typeof(int), type);

csv.WriteField(string.Empty);
Assert.AreEqual(typeof(string), type);
}
}
}
}
4 changes: 2 additions & 2 deletions tests/CsvHelper.Tests/Writing/SanitizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void QuoteTest()
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
SanitizeForInjection = true,
ShouldQuote = (field, context) => false,
ShouldQuote = (_, _, _) => false,
};
using (var writer = new StringWriter())
using (var csv = new CsvWriter(writer, config))
Expand Down Expand Up @@ -73,7 +73,7 @@ public void QuoteChangeEscapeCharacterTest()
{
SanitizeForInjection = true,
InjectionEscapeCharacter = '\'',
ShouldQuote = (field, context) => false,
ShouldQuote = (_, _, _) => false,
};
using (var writer = new StringWriter())
using (var csv = new CsvWriter(writer, config))
Expand Down
8 changes: 4 additions & 4 deletions tests/CsvHelper.Tests/Writing/ShouldQuoteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void QuoteAllFieldsTest()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldQuote = (field, context) => true,
ShouldQuote = (_, _, _) => true,
};
using (var writer = new StringWriter())
using (var csv = new CsvWriter(writer, config))
Expand All @@ -35,7 +35,7 @@ public void QuoteNoFieldsTest()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldQuote = (field, context) => false,
ShouldQuote = (_, _, _) => false,
};
using (var writer = new StringWriter())
using (var csv = new CsvWriter(writer, config))
Expand Down Expand Up @@ -144,11 +144,11 @@ public void Test1()
var data = new List<(int row, int column, string field)>();
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldQuote = (field, row) =>
ShouldQuote = (field, fieldType, row) =>
{
data.Add((row.Row, row.Index, field));
return ConfigurationFunctions.ShouldQuote(field, row);
return ConfigurationFunctions.ShouldQuote(field, fieldType, row);
},
};
using (var writer = new StringWriter())
Expand Down

0 comments on commit ceaa383

Please sign in to comment.