Skip to content

Commit

Permalink
Merge branch 'Rob-Hague-attributeqol'
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshClose committed Jan 25, 2024
2 parents 7eae7d2 + 764c090 commit 4deab1a
Show file tree
Hide file tree
Showing 52 changed files with 624 additions and 342 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,64 @@ Most of the configuration done via class maps can also be done using attributes.
###### Data

```
Identifier,name,IsBool,Constant
1,one,yes,a
2,two,no,b
Identifier||Amount2|IsBool|Constant
1|1,234|1,234|yes|a
2|1.234|1.234|no|b
```

###### Example

```cs
void Main()
{
CsvConfiguration config = CsvConfiguration.FromType<Foo>();
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
using (var csv = new CsvReader(reader, config))
{
csv.GetRecords<Foo>().ToList().Dump();
List<Foo> records = csv.GetRecords<Foo>().ToList();

// These all print "True"
Console.WriteLine(records.Count == 2);
Console.WriteLine(records[0].Id == 1);
Console.WriteLine(records[0].Amount == 1.234m);
Console.WriteLine(records[0].Amount2 == 1234);
Console.WriteLine(records[0].IsBool == true);
Console.WriteLine(records[0].Constant == "bar");
Console.WriteLine(records[0].Optional == null);
Console.WriteLine(records[0].Ignored == null);

Console.WriteLine(records[1].Amount == 1234);
Console.WriteLine(records[1].Amount2 == 1.234m);

}
}

[Delimiter(",")]
[CultureInfo("")] // Set CultureInfo to InvariantCulture
[Delimiter("|")]
[CultureInfo("de-DE")]
public class Foo
{
[Name("Identifier")]
public int Id { get; set; }

[Index(1)]
public string Name { get; set; }
public decimal Amount { get; set; }

[CultureInfo("InvariantCulture")]
public decimal Amount2 { get; set; }

[BooleanTrueValues("yes")]
[BooleanFalseValues("no")]
public bool IsBool { get; set; }

[Constant("bar")]
public string Constant { get; set; }

[Optional]
public string Optional { get; set; }

[Ignore]
public string Ignored { get; set; }
public string Ignored { get; set; }
}

```
10 changes: 5 additions & 5 deletions src/CsvHelper/Configuration/Attributes/AllowCommentsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
namespace CsvHelper.Configuration.Attributes
{
/// <summary>
/// A value indicating if comments are allowed.
/// A value indicating whether comments are allowed.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class AllowCommentsAttribute : Attribute, IClassMapper
{
/// <summary>
/// Gets a value indicating if comments are allowed.
/// Gets a value indicating whether comments are allowed.
/// </summary>
public bool AllowComments { get; private set; }

/// <summary>
/// A value indicating if comments are allowed.
/// A value indicating whether comments are allowed.
/// </summary>
/// <param name="allowComments">The value indicating id comments are allowed.</param>
public AllowCommentsAttribute(bool allowComments)
/// <param name="allowComments">The value indicating whether comments are allowed.</param>
public AllowCommentsAttribute(bool allowComments = true)
{
AllowComments = allowComments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace CsvHelper.Configuration.Attributes
{
/// <summary>
/// Cache fields that are created when parsing.
/// Default is false.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class CacheFieldsAttribute : Attribute, IClassMapper
Expand All @@ -22,7 +21,7 @@ public class CacheFieldsAttribute : Attribute, IClassMapper
/// Cache fields that are created when parsing.
/// </summary>
/// <param name="cacheFields"></param>
public CacheFieldsAttribute(bool cacheFields)
public CacheFieldsAttribute(bool cacheFields = true)
{
CacheFields = cacheFields;
}
Expand Down
8 changes: 4 additions & 4 deletions src/CsvHelper/Configuration/Attributes/CountBytesAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CsvHelper.Configuration.Attributes
{
/// <summary>
/// A value indicating whether the number of bytes should
/// be counted while parsing. Default is false. This will slow down parsing
/// be counted while parsing. This will slow down parsing
/// because it needs to get the byte count of every char for the given encoding.
/// The <see cref="Encoding"/> needs to be set correctly for this to be accurate.
/// </summary>
Expand All @@ -18,20 +18,20 @@ public class CountBytesAttribute : Attribute, IClassMapper
{
/// <summary>
/// A value indicating whether the number of bytes should
/// be counted while parsing. Default is false. This will slow down parsing
/// be counted while parsing. This will slow down parsing
/// because it needs to get the byte count of every char for the given encoding.
/// The <see cref="Encoding"/> needs to be set correctly for this to be accurate.
/// </summary>
public bool CountBytes { get; private set; }

/// <summary>
/// A value indicating whether the number of bytes should
/// be counted while parsing. Default is false. This will slow down parsing
/// be counted while parsing. This will slow down parsing
/// because it needs to get the byte count of every char for the given encoding.
/// The <see cref="Encoding"/> needs to be set correctly for this to be accurate.
/// </summary>
/// <param name="countBytes"></param>
public CountBytesAttribute(bool countBytes)
public CountBytesAttribute(bool countBytes = true)
{
CountBytes = countBytes;
}
Expand Down
50 changes: 37 additions & 13 deletions src/CsvHelper/Configuration/Attributes/CultureInfoAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,51 @@
namespace CsvHelper.Configuration.Attributes
{
/// <summary>
/// The <see cref="CultureInfo"/> used when type converting.
/// This will override the global <see cref="CsvConfiguration.CultureInfo"/>
/// setting. Or set the same if the attribute is specified on class level.
/// When applied to a member, specifies the <see cref="System.Globalization.CultureInfo"/>
/// used when type converting the member. When applied to a type, the value of
/// <see cref="CsvConfiguration.CultureInfo"/> in the <see cref="CsvConfiguration"/>
/// returned by <see cref="CsvConfiguration.FromType{T}()"/>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public class CultureInfoAttribute : Attribute, IMemberMapper, IParameterMapper
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class CultureInfoAttribute : Attribute, IClassMapper, IMemberMapper, IParameterMapper
{
/// <summary>
/// Gets the culture info.
/// </summary>
public CultureInfo CultureInfo { get; private set; }

/// <summary>
/// The <see cref="CultureInfo"/> used when type converting.
/// This will override the global <see cref="CsvConfiguration.CultureInfo"/>
/// setting. Or set the same if the attribute is specified on class level.
/// </summary>
/// <param name="culture">The culture.</param>
public CultureInfoAttribute(string culture)
/// <summary><inheritdoc cref="CultureInfoAttribute"/></summary>
/// <param name="name">
/// The name of a culture (case insensitive), or the literal values <c>"InvariantCulture"</c>,
/// <c>"CurrentCulture"</c>, <c>"CurrentUICulture"</c>, <c>"InstalledUICulture"</c> to use the
/// corresponding static properties on <see cref="System.Globalization.CultureInfo"/>.
/// </param>
public CultureInfoAttribute(string name)
{
switch(name)
{
case nameof(CultureInfo.InvariantCulture):
CultureInfo = CultureInfo.InvariantCulture;
break;
case nameof(CultureInfo.CurrentCulture):
CultureInfo = CultureInfo.CurrentCulture;
break;
case nameof(CultureInfo.CurrentUICulture):
CultureInfo = CultureInfo.CurrentUICulture;
break;
case nameof(CultureInfo.InstalledUICulture):
CultureInfo = CultureInfo.InstalledUICulture;
break;
default:
CultureInfo = CultureInfo.GetCultureInfo(name);
break;
}
}

/// <inheritdoc />
public void ApplyTo(CsvConfiguration configuration)
{
CultureInfo = CultureInfo.GetCultureInfo(culture);
configuration.CultureInfo = CultureInfo;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ namespace CsvHelper.Configuration.Attributes
{
/// <summary>
/// A value indicating whether changes in the column
/// count should be detected. If true, a <see cref="BadDataException"/>
/// count should be detected. If <see langword="true"/>, a <see cref="BadDataException"/>
/// will be thrown if a different column count is detected.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class DetectColumnCountChangesAttribute : Attribute, IClassMapper
{
/// <summary>
/// A value indicating whether changes in the column
/// count should be detected. If true, a <see cref="BadDataException"/>
/// count should be detected. If <see langword="true"/>, a <see cref="BadDataException"/>
/// will be thrown if a different column count is detected.
/// </summary>
public bool DetectColumnCountChanges { get; private set; }

/// <summary>
/// A value indicating whether changes in the column
/// count should be detected. If true, a <see cref="BadDataException"/>
/// count should be detected. If <see langword="true"/>, a <see cref="BadDataException"/>
/// will be thrown if a different column count is detected.
/// </summary>
public DetectColumnCountChangesAttribute(bool detectColumnCountChanges)
public DetectColumnCountChangesAttribute(bool detectColumnCountChanges = true)
{
DetectColumnCountChanges = detectColumnCountChanges;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace CsvHelper.Configuration.Attributes
{
/// <summary>
/// Detect the delimiter instead of using the delimiter from configuration.
/// Default is <c>false</c>.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class DetectDelimiterAttribute : Attribute, IClassMapper
Expand All @@ -21,7 +20,7 @@ public class DetectDelimiterAttribute : Attribute, IClassMapper
/// <summary>
/// Detect the delimiter instead of using the delimiter from configuration.
/// </summary>
public DetectDelimiterAttribute(bool detectDelimiter)
public DetectDelimiterAttribute(bool detectDelimiter = true)
{
DetectDelimiter = detectDelimiter;
}
Expand Down
23 changes: 16 additions & 7 deletions src/CsvHelper/Configuration/Attributes/EncodingAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ public class EncodingAttribute : Attribute, IClassMapper
/// </summary>
public Encoding Encoding { get; private set; }

/// <summary>
/// The encoding used when counting bytes.
/// </summary>
/// <param name="encoding">The encoding.</param>
public EncodingAttribute(string encoding)
/// <summary>
/// The encoding used when counting bytes.
/// </summary>
/// <param name="name"><inheritdoc cref="Encoding.GetEncoding(string)"/></param>
public EncodingAttribute(string name)
{
Encoding = Encoding.GetEncoding(encoding);
}
Encoding = Encoding.GetEncoding(name);
}

/// <summary>
/// The encoding used when counting bytes.
/// </summary>
/// <param name="codepage"><inheritdoc cref="Encoding.GetEncoding(int)"/></param>
public EncodingAttribute(int codepage)
{
Encoding = Encoding.GetEncoding(codepage);
}

/// <inheritdoc />
public void ApplyTo(CsvConfiguration configuration)
Expand Down
4 changes: 2 additions & 2 deletions src/CsvHelper/Configuration/Attributes/EscapeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CsvHelper.Configuration.Attributes
/// <summary>
/// The escape character used to escape a quote inside a field.
/// </summary>
[AttributeUsage( AttributeTargets.Class, AllowMultiple = false, Inherited = true )]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class EscapeAttribute : Attribute, IClassMapper
{
/// <summary>
Expand All @@ -21,7 +21,7 @@ public class EscapeAttribute : Attribute, IClassMapper
/// The escape character used to escape a quote inside a field.
/// </summary>
/// <param name="escape">The escape character.</param>
public EscapeAttribute( char escape )
public EscapeAttribute(char escape)
{
Escape = escape;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@
namespace CsvHelper.Configuration.Attributes
{
/// <summary>
/// A value indicating if exception messages contain raw CSV data.
/// <c>true</c> if exception contain raw CSV data, otherwise <c>false</c>.
/// Default is <c>true</c>.
/// A value indicating whether exception messages contain raw CSV data.
/// <see langword="true"/> if exceptions contain raw CSV data, otherwise <see langword="false"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ExceptionMessagesContainRawDataAttribute : Attribute, IClassMapper
{
/// <summary>
/// A value indicating if exception messages contain raw CSV data.
/// <c>true</c> if exception contain raw CSV data, otherwise <c>false</c>.
/// A value indicating whether exception messages contain raw CSV data.
/// <see langword="true"/> if exceptions contain raw CSV data, otherwise <see langword="false"/>.
/// </summary>
public bool ExceptionMessagesContainRawData { get; private set; }

/// <summary>
/// A value indicating if exception messages contain raw CSV data.
/// <c>true</c> if exception contain raw CSV data, otherwise <c>false</c>.
/// A value indicating whether exception messages contain raw CSV data.
/// <see langword="true"/> if exceptions contain raw CSV data, otherwise <see langword="false"/>.
/// </summary>
/// <param name="exceptionMessagesContainRawData"></param>
public ExceptionMessagesContainRawDataAttribute(bool exceptionMessagesContainRawData)
public ExceptionMessagesContainRawDataAttribute(bool exceptionMessagesContainRawData = true)
{
ExceptionMessagesContainRawData = exceptionMessagesContainRawData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
namespace CsvHelper.Configuration.Attributes
{
/// <summary>
/// A value indicating if the CSV file has a header record.
/// A value indicating whether the CSV file has a header record.
/// </summary>
[AttributeUsage( AttributeTargets.Class, AllowMultiple = false, Inherited = true )]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class HasHeaderRecordAttribute : Attribute, IClassMapper
{
/// <summary>
/// Gets a value indicating if the CSV file has a header record.
/// Gets a value indicating whether the CSV file has a header record.
/// </summary>
public bool HasHeaderRecord { get; private set; }

/// <summary>
/// A value indicating if the CSV file has a header record.
/// A value indicating whether the CSV file has a header record.
/// </summary>
/// <param name="hasHeaderRecord">A value indicating if the CSV file has a header record.</param>
public HasHeaderRecordAttribute( bool hasHeaderRecord )
/// <param name="hasHeaderRecord">A value indicating whether the CSV file has a header record.</param>
public HasHeaderRecordAttribute(bool hasHeaderRecord = true)
{
HasHeaderRecord = hasHeaderRecord;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HeaderPrefixAttribute : Attribute, IMemberReferenceMapper, IParamet
public string Prefix { get; private set; }

/// <summary>
/// Gets a value indicating if the prefix should inherit parent prefixes.
/// Gets a value indicating whether the prefix should inherit parent prefixes.
/// </summary>
public bool Inherit { get; private set; }

Expand Down
Loading

0 comments on commit 4deab1a

Please sign in to comment.