Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source generator - ColumnIgnore #81

Merged
merged 6 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<GlobalPackageReference Include="ErrorProne.NET.CoreAnalyzers" Version="0.4.0-beta.1" />
<GlobalPackageReference Include="Meziantou.Analyzer" Version="2.0.176" />
<GlobalPackageReference Include="Meziantou.Analyzer" Version="2.0.177" />
<GlobalPackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4" />
<GlobalPackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.11.20" />
<GlobalPackageReference Include="PrimaryConstructorAnalyzer" Version="1.0.6" />
Expand Down Expand Up @@ -35,7 +35,7 @@
<PackageVersion Include="System.Reflection.Metadata" Version="8.0.1" />
<PackageVersion Include="TngTech.ArchUnitNET.xUnit" Version="0.11.1" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageVersion Include="Verify.Xunit" Version="27.1.0" />
<PackageVersion Include="Verify.Xunit" Version="28.1.3" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.assert" Version="2.9.2" />
<PackageVersion Include="Xunit.Combinatorial" Version="1.6.24" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using SpreadCheetah.SourceGenerator.SnapshotTest.Helpers;

namespace SpreadCheetah.SourceGenerator.SnapshotTest.Tests;

public class ColumnIgnoreTests
{
[Fact]
public Task ColumnIgnore_ClassPropertyWithAnotherSpreadCheetahAttribute()
{
// Arrange
var context = AnalyzerTest.CreateContext();
context.TestCode = """
using SpreadCheetah.SourceGeneration;

namespace MyNamespace;

public class ClassWithColumnIgnore
{
[{|SPCH1008:CellFormat("#.0#")|}]
[ColumnIgnore]
[{|SPCH1008:ColumnWidth(10)|}]
public decimal Value { get; set; }
}

[WorksheetRow(typeof(ClassWithColumnIgnore))]
public partial class MyGenRowContext : WorksheetRowContext;
""";

// Act & Assert
return context.RunAsync();
}

[Fact]
public Task ColumnIgnore_ClassPropertyWithAnotherUnrelatedAttribute()
{
// Arrange
var context = AnalyzerTest.CreateContext();
context.TestCode = """
using SpreadCheetah.SourceGeneration;
using System.ComponentModel.DataAnnotations;

namespace MyNamespace;

public class ClassWithColumnIgnore
{
[Required]
[ColumnIgnore]
public decimal Value { get; set; }
}

[WorksheetRow(typeof(ClassWithColumnIgnore))]
public partial class MyGenRowContext : WorksheetRowContext;
""";

// Act & Assert
return context.RunAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SpreadCheetah.SourceGeneration;

namespace SpreadCheetah.SourceGenerator.Test.Models.ColumnIgnore;

public class ClassWithMultipleProperties
{
[ColumnIgnore]
public int Id { get; set; }

public string? Name { get; set; }

[ColumnIgnore]
public decimal Price { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using SpreadCheetah.SourceGeneration;

namespace SpreadCheetah.SourceGenerator.Test.Models.ColumnIgnore;

[WorksheetRow(typeof(ClassWithMultipleProperties))]
public partial class ColumnIgnoreContext : WorksheetRowContext;
32 changes: 32 additions & 0 deletions SpreadCheetah.SourceGenerator.Test/Tests/ColumnIgnoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using SpreadCheetah.SourceGenerator.Test.Models.ColumnIgnore;
using SpreadCheetah.TestHelpers.Assertions;
using Xunit;

namespace SpreadCheetah.SourceGenerator.Test.Tests;

public class ColumnIgnoreTests
{
[Fact]
public async Task ColumnIgnore_ClassWithMultipleProperties()
{
// Arrange
using var stream = new MemoryStream();
await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream);
await spreadsheet.StartWorksheetAsync("Sheet");
var obj = new ClassWithMultipleProperties
{
Id = 1,
Name = "Foo",
Price = 199.90m
};

// Act
await spreadsheet.AddAsRowAsync(obj, ColumnIgnoreContext.Default.ClassWithMultipleProperties);
await spreadsheet.FinishAsync();

// Assert
using var sheet = SpreadsheetAssert.SingleSheet(stream);
Assert.Equal(obj.Name, sheet["A1"].StringValue);
Assert.Single(sheet.Columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private static int GetOrder(string? metadataName)
{
return metadataName switch
{
Attributes.ColumnIgnore => -2,
Attributes.CellValueConverter => -1,
Attributes.CellStyle => 1,
_ => 0
Expand Down
1 change: 1 addition & 0 deletions SpreadCheetah.SourceGenerator/Helpers/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal static class Attributes
public const string CellValueConverter = "CellValueConverterAttribute";
public const string CellValueTruncate = "CellValueTruncateAttribute";
public const string ColumnHeader = "ColumnHeaderAttribute";
public const string ColumnIgnore = "ColumnIgnoreAttribute";
public const string ColumnOrder = "ColumnOrderAttribute";
public const string ColumnWidth = "ColumnWidthAttribute";
public const string GenerationOptions = "WorksheetRowGenerationOptionsAttribute";
Expand Down
13 changes: 13 additions & 0 deletions SpreadCheetah.SourceGenerator/Helpers/PropertyAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ public PropertyAttributeData Analyze(IPropertySymbol property, CancellationToken

foreach (var attribute in attributes)
{
if (_result.ColumnIgnore is not null)
{
diagnostics.ReportAttributeCombinationNotSupported(attribute, Attributes.ColumnIgnore, token);
continue;
}

_ = attribute.AttributeClass?.MetadataName switch
{
Attributes.CellFormat => TryGetCellFormatAttribute(attribute, token),
Attributes.CellStyle => TryGetCellStyleAttribute(attribute, token),
Attributes.CellValueConverter => TryGetCellValueConverterAttribute(attribute, property.Type, token),
Attributes.CellValueTruncate => TryGetCellValueTruncateAttribute(attribute, property.Type, token),
Attributes.ColumnHeader => TryGetColumnHeaderAttribute(attribute, token),
Attributes.ColumnIgnore => TryGetColumnIgnoreAttribute(),
Attributes.ColumnOrder => TryGetColumnOrderAttribute(attribute),
Attributes.ColumnWidth => TryGetColumnWidthAttribute(attribute, token),
_ => false
Expand Down Expand Up @@ -204,6 +211,12 @@ private bool TryGetColumnHeaderAttribute(
return null;
}

private bool TryGetColumnIgnoreAttribute()
{
_result.ColumnIgnore = new ColumnIgnore();
return true;
}

private bool TryGetColumnOrderAttribute(AttributeData attribute)
{
var args = attribute.ConstructorArguments;
Expand Down
3 changes: 3 additions & 0 deletions SpreadCheetah.SourceGenerator/Models/ColumnIgnore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace SpreadCheetah.SourceGenerator.Models;

internal readonly record struct ColumnIgnore;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal record struct PropertyAttributeData
public CellValueConverter? CellValueConverter { get; set; }
public CellValueTruncate? CellValueTruncate { get; set; }
public ColumnHeader? ColumnHeader { get; set; }
public ColumnIgnore? ColumnIgnore { get; set; }
public ColumnOrder? ColumnOrder { get; set; }
public ColumnWidth? ColumnWidth { get; set; }
}
3 changes: 2 additions & 1 deletion SpreadCheetah.SourceGenerator/WorksheetRowGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
foreach (var property in properties)
{
var data = analyzer.Analyze(property, token);

if (data.ColumnIgnore is not null)
continue;

Check warning on line 85 in SpreadCheetah.SourceGenerator/WorksheetRowGenerator.cs

View check run for this annotation

Codecov / codecov/patch

SpreadCheetah.SourceGenerator/WorksheetRowGenerator.cs#L85

Added line #L85 was not covered by tests
if (data.CellValueConverter is null && !property.Type.IsSupportedType())
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ namespace SpreadCheetah.SourceGeneration
public ColumnHeaderAttribute(System.Type type, string propertyName) { }
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class ColumnIgnoreAttribute : System.Attribute
{
public ColumnIgnoreAttribute() { }
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class ColumnOrderAttribute : System.Attribute
{
public ColumnOrderAttribute(int order) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ namespace SpreadCheetah.SourceGeneration
public ColumnHeaderAttribute(System.Type type, string propertyName) { }
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class ColumnIgnoreAttribute : System.Attribute
{
public ColumnIgnoreAttribute() { }
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class ColumnOrderAttribute : System.Attribute
{
public ColumnOrderAttribute(int order) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ namespace SpreadCheetah.SourceGeneration
public ColumnHeaderAttribute(System.Type type, string propertyName) { }
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class ColumnIgnoreAttribute : System.Attribute
{
public ColumnIgnoreAttribute() { }
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class ColumnOrderAttribute : System.Attribute
{
public ColumnOrderAttribute(int order) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ namespace SpreadCheetah.SourceGeneration
public ColumnHeaderAttribute(System.Type type, string propertyName) { }
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class ColumnIgnoreAttribute : System.Attribute
{
public ColumnIgnoreAttribute() { }
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class ColumnOrderAttribute : System.Attribute
{
public ColumnOrderAttribute(int order) { }
Expand Down
7 changes: 7 additions & 0 deletions SpreadCheetah/SourceGeneration/ColumnIgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SpreadCheetah.SourceGeneration;

/// <summary>
/// Instructs the SpreadCheetah source generator to ignore a property.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class ColumnIgnoreAttribute : Attribute;
Loading