Skip to content

Commit

Permalink
Version 2.0.6: Add CsvIgnore attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Nov 13, 2024
1 parent a5fb7fe commit 63865f0
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 16 deletions.
10 changes: 5 additions & 5 deletions Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="MessagePack" Version="2.5.129" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="MessagePack" Version="2.5.192" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RecordParser" Version="2.1.0" />
<PackageReference Include="RecordParser" Version="2.3.0" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions CsvSerializer/CsvSerializer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageId>RG.CsvSerializer</PackageId>
<Authors>Ronny Gunawan</Authors>
<Copyright>(c) 2019-2023 Ronny Gunawan</Copyright>
<Copyright>(c) 2019-2024 Ronny Gunawan</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/ronnygunawan/csv-serializer</PackageProjectUrl>
<RepositoryUrl>https://github.com/ronnygunawan/csv-serializer</RepositoryUrl>
<PackageTags>csv serializer deserializer parser core</PackageTags>
<Description>Fast CSV to object serializer and deserializer.</Description>
<Version>2.0.5</Version>
<Version>2.0.6</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
Expand Down Expand Up @@ -46,8 +46,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.7.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion CsvSerializer/Internals/NativeImplGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ List<Location> invocationLocations

List<IPropertySymbol> propertySymbols = typeSymbol.GetMembers()
.OfType<IPropertySymbol>()
.Where(prop => prop.DeclaredAccessibility == Accessibility.Public && !prop.IsStatic)
.Where(prop => prop.DeclaredAccessibility == Accessibility.Public
&& !prop.IsStatic
&& !prop.GetAttributes().Any(attr => attr.AttributeClass?.Name == "CsvIgnoreAttribute"))
.ToList();

StringBuilder serializeHeaderBuilder = new();
Expand Down
3 changes: 3 additions & 0 deletions CsvSerializer/Internals/StaticSourceFilesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public CsvColumnAttribute(string name) {
Name = name;
}
}
[AttributeUsage(AttributeTargets.Property)]
public sealed class CsvIgnoreAttribute : Attribute { }
}
""",
Expand Down
2 changes: 2 additions & 0 deletions CsvSerializer/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using Csv.Internals;
using Microsoft.CodeAnalysis;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace CsvSerializer {
#pragma warning restore IDE0130 // Namespace does not match folder structure
[Generator]
public class SourceGenerator : ISourceGenerator {
public void Initialize(GeneratorInitializationContext context) {
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Product {

[CsvColumn("Added", DateFormat = "dd/MM/yyyy")]
public DateTime Added { get; set; }

[CsvIgnore]
public Category Category { get; set; }
}
```

Expand Down
23 changes: 23 additions & 0 deletions Tests/NaiveSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,28 @@ public sealed record ModelWithNullableValues {
public string? String { get; set; }
public DateTime? DateTime { get; set; }
}

public sealed record ModelWithIgnoredProperties {
[CsvIgnore]
public int Id { get; set; }

public bool? Bool { get; set; }
public byte? Byte { get; set; }
public sbyte? SByte { get; set; }
public short? Short { get; set; }
public ushort? UShort { get; set; }
public int? Int { get; set; }
public uint? UInt { get; set; }
public long? Long { get; set; }
public ulong? ULong { get; set; }
public float? Float { get; set; }
public double? Double { get; set; }
public decimal? Decimal { get; set; }

[CsvIgnore]
public string? String { get; set; }

public DateTime? DateTime { get; set; }
}
}
}
72 changes: 72 additions & 0 deletions Tests/NativeSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,55 @@ public void CommasInStringDontSplitString() {
items[1].Name.Should().Be("Banner, Bruce");
items[1].LastName.Should().Be("Banner");
}

[Fact]
public void CanIgnoreProperties() {
typeof(ModelWithIgnoredProperties).IsPublic.Should().BeTrue();

string csv = ",,,,,,,,,,,,";

ModelWithIgnoredProperties[] items = CsvSerializer.Deserialize<ModelWithIgnoredProperties>(csv, provider: CultureInfo.GetCultureInfo("en-US"));
items.Length.Should().Be(1);
ModelWithIgnoredProperties item = items[0];
item.Id.Should().Be(0);
item.Bool.Should().BeNull();
item.Byte.Should().BeNull();
item.SByte.Should().BeNull();
item.Short.Should().BeNull();
item.UShort.Should().BeNull();
item.Int.Should().BeNull();
item.UInt.Should().BeNull();
item.Long.Should().BeNull();
item.ULong.Should().BeNull();
item.Float.Should().BeNull();
item.Double.Should().BeNull();
item.Decimal.Should().BeNull();
item.String.Should().BeNull();
item.DateTime.Should().BeNull();

csv = """
"Bool","Byte","SByte","Short","UShort","Int","UInt","Long","ULong","Float","Double","Decimal","DateTime"
True,102,-100,-200,200,-3000,3000,-40000,40000,1E+14,1.7837193718273812E+19,989898989898,"08/23/2019 00:00:00"
""";
items = CsvSerializer.Deserialize<ModelWithIgnoredProperties>(csv, hasHeaders: true, provider: CultureInfo.GetCultureInfo("en-US"));
items.Length.Should().Be(1);
item = items.Single();
item.Id.Should().Be(0);
item.Bool.Should().BeTrue();
item.Byte.Should().Be(0x66);
item.SByte.Should().Be(-100);
item.Short.Should().Be(-200);
item.UShort.Should().Be(200);
item.Int.Should().Be(-3000);
item.UInt.Should().Be(3000);
item.Long.Should().Be(-40000L);
item.ULong.Should().Be(40000UL);
item.Float.Should().Be(100000000000000.0f);
item.Double.Should().Be(17837193718273812973.0);
item.Decimal.Should().Be(989898989898m);
item.String.Should().BeNull();
item.DateTime.Should().Be(new DateTime(2019, 8, 23));
}
}

public class ModelWithNullableValues {
Expand All @@ -160,6 +209,29 @@ public class ModelWithNullableValues {
public DateTime? DateTime { get; set; }
}

public class ModelWithIgnoredProperties {
[CsvIgnore]
public int Id { get; set; }

public bool? Bool { get; set; }
public byte? Byte { get; set; }
public sbyte? SByte { get; set; }
public short? Short { get; set; }
public ushort? UShort { get; set; }
public int? Int { get; set; }
public uint? UInt { get; set; }
public long? Long { get; set; }
public ulong? ULong { get; set; }
public float? Float { get; set; }
public double? Double { get; set; }
public decimal? Decimal { get; set; }

[CsvIgnore]
public string? String { get; set; }

public DateTime? DateTime { get; set; }
}

public class EscapeTest {
public string? Name { get; set; }
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 63865f0

Please sign in to comment.