diff --git a/.editorconfig b/.editorconfig index ff3ff70b..c468c5b9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -156,7 +156,7 @@ dotnet_diagnostic.IDE0301.severity = warning # IDE0305: Use collection expression for fluent dotnet_diagnostic.IDE0305.severity = warning -[{CellValueTruncateAttribute.cs,ColumnHeaderAttribute.cs,ColumnOrderAttribute.cs,ColumnWidthAttribute.cs,WorksheetRowAttribute.cs}] +[{CellValueTruncateAttribute.cs,ColumnHeaderAttribute.cs,ColumnOrderAttribute.cs,CellStyleAttribute.cs,ColumnWidthAttribute.cs,WorksheetRowAttribute.cs}] # CA1019: Define accessors for attribute arguments dotnet_diagnostic.CA1019.severity = none # CS9113: Parameter is unread @@ -244,6 +244,9 @@ dotnet_diagnostic.MA0076.severity = none # RCS1163: Unused parameter dotnet_diagnostic.RCS1163.severity = warning +# RCS1205: Order named arguments according to the order of parameters +dotnet_diagnostic.RCS1205.severity = none + [{ColumnHeaderAttribute.cs,ColumnOrderAttribute.cs,WorksheetRowAttribute.cs}] # RCS1163: Unused parameter dotnet_diagnostic.RCS1163.severity = none diff --git a/Directory.Packages.props b/Directory.Packages.props index 3ad67adc..739839ee 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -23,8 +23,7 @@ - - + diff --git a/SpreadCheetah.Benchmark/SpreadCheetah.Benchmark.csproj b/SpreadCheetah.Benchmark/SpreadCheetah.Benchmark.csproj index db605f8d..6e2e1753 100644 --- a/SpreadCheetah.Benchmark/SpreadCheetah.Benchmark.csproj +++ b/SpreadCheetah.Benchmark/SpreadCheetah.Benchmark.csproj @@ -10,7 +10,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/SpreadCheetah.SourceGenerator.CSharp8Test/Models/BaseClass.cs b/SpreadCheetah.SourceGenerator.CSharp8Test/Models/BaseClass.cs index 754009e9..6ed216df 100644 --- a/SpreadCheetah.SourceGenerator.CSharp8Test/Models/BaseClass.cs +++ b/SpreadCheetah.SourceGenerator.CSharp8Test/Models/BaseClass.cs @@ -1,7 +1,10 @@ -namespace SpreadCheetah.SourceGenerator.CSharp8Test.Models +using SpreadCheetah.SourceGeneration; + +namespace SpreadCheetah.SourceGenerator.CSharp8Test.Models { public class BaseClass { + [CellStyle("Id style")] public string Id { get; } protected BaseClass(string id) diff --git a/SpreadCheetah.SourceGenerator.CSharp8Test/Models/ClassWithMultipleProperties.cs b/SpreadCheetah.SourceGenerator.CSharp8Test/Models/ClassWithMultipleProperties.cs index 983b172f..6a8af5af 100644 --- a/SpreadCheetah.SourceGenerator.CSharp8Test/Models/ClassWithMultipleProperties.cs +++ b/SpreadCheetah.SourceGenerator.CSharp8Test/Models/ClassWithMultipleProperties.cs @@ -6,10 +6,13 @@ namespace SpreadCheetah.SourceGenerator.CSharp8Test.Models public class ClassWithMultipleProperties : BaseClass { [ColumnHeader("Last name")] + [CellStyle("Last name style")] [CellValueTruncate(20)] public string LastName { get; } + [ColumnOrder(1)] public string FirstName { get; } + [ColumnWidth(5)] public int Age { get; } diff --git a/SpreadCheetah.SourceGenerator.CSharp8Test/Tests/WorksheetRowGeneratorTests.cs b/SpreadCheetah.SourceGenerator.CSharp8Test/Tests/WorksheetRowGeneratorTests.cs index aac7dea3..f481b2f8 100644 --- a/SpreadCheetah.SourceGenerator.CSharp8Test/Tests/WorksheetRowGeneratorTests.cs +++ b/SpreadCheetah.SourceGenerator.CSharp8Test/Tests/WorksheetRowGeneratorTests.cs @@ -1,4 +1,5 @@ using SpreadCheetah.SourceGenerator.CSharp8Test.Models; +using SpreadCheetah.Styling; using SpreadCheetah.TestHelpers.Assertions; using System; using System.IO; @@ -24,6 +25,8 @@ public async Task Spreadsheet_AddAsRow_ClassWithMultipleProperties() await using (var spreadsheet = await Spreadsheet.CreateNewAsync(stream)) { await spreadsheet.StartWorksheetAsync("Sheet", ctx); + spreadsheet.AddStyle(new Style { Font = { Bold = true } }, "Id style"); + spreadsheet.AddStyle(new Style { Font = { Italic = true } }, "Last name style"); // Act await spreadsheet.AddAsRowAsync(obj, ctx); @@ -39,6 +42,9 @@ public async Task Spreadsheet_AddAsRow_ClassWithMultipleProperties() Assert.Equal(obj.Age, sheet["D1"].IntValue); Assert.Equal(4, sheet.CellCount); Assert.Equal(5, sheet.Column("D").Width, 4); + + Assert.True(sheet["B1"].Style.Font.Bold); + Assert.True(sheet["C1"].Style.Font.Italic); } [Fact] diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Helpers/TestHelper.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Helpers/TestHelper.cs index fa7bb0cc..110de321 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Helpers/TestHelper.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Helpers/TestHelper.cs @@ -26,7 +26,10 @@ private static PortableExecutableReference[] GetAssemblyReferences() ]; } - public static SettingsTask CompileAndVerify(string source, bool replaceEscapedLineEndings = false, params object?[] parameters) where T : IIncrementalGenerator, new() + public static SettingsTask CompileAndVerify(string source, + bool replaceEscapedLineEndings = false, + bool onlyDiagnostics = false, + params object?[] parameters) where T : IIncrementalGenerator, new() { var syntaxTree = CSharpSyntaxTree.ParseText(source); var references = GetAssemblyReferences(); @@ -47,6 +50,9 @@ private static PortableExecutableReference[] GetAssemblyReferences() var task = Verify(target, settings); + if (onlyDiagnostics) + task = task.IgnoreGeneratedResult(x => x.SourceText.ToString().Contains("public WorksheetRowTypeInfo<", StringComparison.Ordinal)); + return parameters.Length > 0 ? task.UseParameters(parameters) : task; diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ClassWithEmptyCellStyle.verified.txt b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ClassWithEmptyCellStyle.verified.txt new file mode 100644 index 00000000..c9f77694 --- /dev/null +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ClassWithEmptyCellStyle.verified.txt @@ -0,0 +1,14 @@ +{ + Diagnostics: [ + { + Id: SPCH1006, + Title: Invalid attribute argument, + Severity: Error, + WarningLevel: 0, + Location: : (6,5)-(6,18), + MessageFormat: '{0}' is an invalid argument for attribute '{1}', + Message: '' is an invalid argument for attribute 'SpreadCheetah.SourceGeneration.CellStyleAttribute', + Category: SpreadCheetah.SourceGenerator + } + ] +} \ No newline at end of file diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ClassWithMultipleAttributes#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ClassWithMultipleAttributes#MyNamespace.MyGenRowContext.g.verified.cs new file mode 100644 index 00000000..8ca91882 --- /dev/null +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ClassWithMultipleAttributes#MyNamespace.MyGenRowContext.g.verified.cs @@ -0,0 +1,134 @@ +//HintName: MyNamespace.MyGenRowContext.g.cs +// +#nullable enable +using SpreadCheetah; +using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace MyNamespace +{ + public partial class MyGenRowContext + { + private static MyGenRowContext? _default; + public static MyGenRowContext Default => _default ??= new MyGenRowContext(); + + public MyGenRowContext() + { + } + + private WorksheetRowTypeInfo? _ClassWithCellStyle; + public WorksheetRowTypeInfo ClassWithCellStyle => _ClassWithCellStyle + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null, CreateWorksheetRowDependencyInfo0); + + private static WorksheetRowDependencyInfo CreateWorksheetRowDependencyInfo0(Spreadsheet spreadsheet) + { + var styleIds = new[] + { + spreadsheet.GetStyleId("Italic"), + spreadsheet.GetStyleId("Bold"), + }; + return new WorksheetRowDependencyInfo(styleIds); + } + + private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(4); + try + { + cells[0] = new StyledCell("FirstName", styleId); + cells[1] = new StyledCell("LastName", styleId); + cells[2] = new StyledCell("YearOfBirth", styleId); + cells[3] = new StyledCell("Initials", styleId); + await spreadsheet.AddRowAsync(cells.AsMemory(0, 4), token).ConfigureAwait(false); + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellStyle? obj, CancellationToken token) + { + if (spreadsheet is null) + throw new ArgumentNullException(nameof(spreadsheet)); + if (obj is null) + return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); + return AddAsRowInternalAsync(spreadsheet, obj, token); + } + + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) + { + if (spreadsheet is null) + throw new ArgumentNullException(nameof(spreadsheet)); + if (objs is null) + throw new ArgumentNullException(nameof(objs)); + return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); + } + + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellStyle obj, + CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(4); + try + { + var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.ClassWithCellStyle); + var styleIds = worksheetRowDependencyInfo.StyleIds; + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(4); + try + { + var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.ClassWithCellStyle); + var styleIds = worksheetRowDependencyInfo.StyleIds; + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellStyle? obj, + StyledCell[] cells, IReadOnlyList styleIds, CancellationToken token) + { + if (obj is null) + return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); + + cells[0] = new StyledCell(new DataCell(obj.FirstName), styleIds[0]); + cells[1] = new StyledCell(new DataCell(obj.LastName), styleIds[1]); + cells[2] = new StyledCell(new DataCell(obj.YearOfBirth), null); + cells[3] = new StyledCell(new DataCell(obj.Initials), styleIds[1]); + return spreadsheet.AddRowAsync(cells.AsMemory(0, 4), token); + } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } + } +} diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ContextWithTwoClassesWithCellStyle#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ContextWithTwoClassesWithCellStyle#MyNamespace.MyGenRowContext.g.verified.cs new file mode 100644 index 00000000..893e2785 --- /dev/null +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.CellStyle_ContextWithTwoClassesWithCellStyle#MyNamespace.MyGenRowContext.g.verified.cs @@ -0,0 +1,233 @@ +//HintName: MyNamespace.MyGenRowContext.g.cs +// +#nullable enable +using SpreadCheetah; +using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace MyNamespace +{ + public partial class MyGenRowContext + { + private static MyGenRowContext? _default; + public static MyGenRowContext Default => _default ??= new MyGenRowContext(); + + public MyGenRowContext() + { + } + + private WorksheetRowTypeInfo? _ClassWithCellStyle; + public WorksheetRowTypeInfo ClassWithCellStyle => _ClassWithCellStyle + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null, CreateWorksheetRowDependencyInfo0); + + private static WorksheetRowDependencyInfo CreateWorksheetRowDependencyInfo0(Spreadsheet spreadsheet) + { + var styleIds = new[] + { + spreadsheet.GetStyleId("Italic"), + spreadsheet.GetStyleId("Bold"), + }; + return new WorksheetRowDependencyInfo(styleIds); + } + + private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(4); + try + { + cells[0] = new StyledCell("FirstName", styleId); + cells[1] = new StyledCell("LastName", styleId); + cells[2] = new StyledCell("YearOfBirth", styleId); + cells[3] = new StyledCell("Initials", styleId); + await spreadsheet.AddRowAsync(cells.AsMemory(0, 4), token).ConfigureAwait(false); + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellStyle? obj, CancellationToken token) + { + if (spreadsheet is null) + throw new ArgumentNullException(nameof(spreadsheet)); + if (obj is null) + return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); + return AddAsRowInternalAsync(spreadsheet, obj, token); + } + + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) + { + if (spreadsheet is null) + throw new ArgumentNullException(nameof(spreadsheet)); + if (objs is null) + throw new ArgumentNullException(nameof(objs)); + return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); + } + + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellStyle obj, + CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(4); + try + { + var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.ClassWithCellStyle); + var styleIds = worksheetRowDependencyInfo.StyleIds; + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(4); + try + { + var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.ClassWithCellStyle); + var styleIds = worksheetRowDependencyInfo.StyleIds; + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellStyle? obj, + StyledCell[] cells, IReadOnlyList styleIds, CancellationToken token) + { + if (obj is null) + return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); + + cells[0] = new StyledCell(new DataCell(obj.FirstName), styleIds[0]); + cells[1] = new StyledCell(new DataCell(obj.LastName), styleIds[1]); + cells[2] = new StyledCell(new DataCell(obj.YearOfBirth), null); + cells[3] = new StyledCell(new DataCell(obj.Initials), styleIds[1]); + return spreadsheet.AddRowAsync(cells.AsMemory(0, 4), token); + } + + private WorksheetRowTypeInfo? _Class2WithCellStyle; + public WorksheetRowTypeInfo Class2WithCellStyle => _Class2WithCellStyle + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow1Async, AddAsRowAsync, AddRangeAsRowsAsync, null, CreateWorksheetRowDependencyInfo1); + + private static WorksheetRowDependencyInfo CreateWorksheetRowDependencyInfo1(Spreadsheet spreadsheet) + { + var styleIds = new[] + { + spreadsheet.GetStyleId("Red"), + spreadsheet.GetStyleId("Bold"), + }; + return new WorksheetRowDependencyInfo(styleIds); + } + + private static async ValueTask AddHeaderRow1Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(2); + try + { + cells[0] = new StyledCell("Id", styleId); + cells[1] = new StyledCell("Price", styleId); + await spreadsheet.AddRowAsync(cells.AsMemory(0, 2), token).ConfigureAwait(false); + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.Class2WithCellStyle? obj, CancellationToken token) + { + if (spreadsheet is null) + throw new ArgumentNullException(nameof(spreadsheet)); + if (obj is null) + return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); + return AddAsRowInternalAsync(spreadsheet, obj, token); + } + + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) + { + if (spreadsheet is null) + throw new ArgumentNullException(nameof(spreadsheet)); + if (objs is null) + throw new ArgumentNullException(nameof(objs)); + return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); + } + + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.Class2WithCellStyle obj, + CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(2); + try + { + var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.Class2WithCellStyle); + var styleIds = worksheetRowDependencyInfo.StyleIds; + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) + { + var cells = ArrayPool.Shared.Rent(2); + try + { + var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.Class2WithCellStyle); + var styleIds = worksheetRowDependencyInfo.StyleIds; + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } + } + finally + { + ArrayPool.Shared.Return(cells, true); + } + } + + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.Class2WithCellStyle? obj, + StyledCell[] cells, IReadOnlyList styleIds, CancellationToken token) + { + if (obj is null) + return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); + + cells[0] = new StyledCell(new DataCell(obj.Id), styleIds[0]); + cells[1] = new StyledCell(new DataCell(obj.Price), styleIds[1]); + return spreadsheet.AddRowAsync(cells.AsMemory(0, 2), token); + } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } + } +} diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_CachingCorrectly.verified.txt b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_CachingCorrectly.verified.txt index 9e7ee9e0..61af5b68 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_CachingCorrectly.verified.txt +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_CachingCorrectly.verified.txt @@ -2,6 +2,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -21,7 +22,8 @@ namespace MyNamespace private WorksheetRowTypeInfo? _ClassWithSingleProperty; public WorksheetRowTypeInfo ClassWithSingleProperty => _ClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -46,7 +48,9 @@ namespace MyNamespace return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -55,12 +59,15 @@ namespace MyNamespace return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -68,12 +75,18 @@ namespace MyNamespace } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -81,15 +94,9 @@ namespace MyNamespace } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -97,5 +104,12 @@ namespace MyNamespace cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithAllSupportedTypes#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithAllSupportedTypes#MyNamespace.MyGenRowContext.g.verified.cs index 214e9352..0f57f313 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithAllSupportedTypes#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithAllSupportedTypes#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithAllSupportedTypes; public WorksheetRowTypeInfo ClassWithAllSupportedTypes => _ClassWithAllSupportedTypes - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -62,7 +64,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -71,12 +75,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithAllSupportedTypes obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithAllSupportedTypes obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(16); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -84,12 +91,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(16); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -97,15 +110,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithAllSupportedTypes? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithAllSupportedTypes? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -128,5 +135,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[15] = new DataCell(obj.NullableBoolValue); return spreadsheet.AddRowAsync(cells.AsMemory(0, 16), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncate#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncate#MyNamespace.MyGenRowContext.g.verified.cs index bb0fe16c..b43b200f 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncate#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncate#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithCellValueTruncate; public WorksheetRowTypeInfo ClassWithCellValueTruncate => _ClassWithCellValueTruncate - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellValueTruncate obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellValueTruncate obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,22 +95,22 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellValueTruncate? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellValueTruncate? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); - var p0 = obj.Name; - cells[0] = p0 is null || p0.Length <= 10 ? new DataCell(p0) : new DataCell(p0.AsMemory(0, 10)); + cells[0] = ConstructTruncatedDataCell(obj.Name, 10); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncateOnInvalidType#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncateOnInvalidType#MyNamespace.MyGenRowContext.g.verified.cs index 43fa0662..1362233d 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncateOnInvalidType#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncateOnInvalidType#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithCellValueTruncate; public WorksheetRowTypeInfo ClassWithCellValueTruncate => _ClassWithCellValueTruncate - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellValueTruncate obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellValueTruncate obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellValueTruncate? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellValueTruncate? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Year); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncateWithInvalidLength#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncateWithInvalidLength#MyNamespace.MyGenRowContext.g.verified.cs index a1af3c34..661850d0 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncateWithInvalidLength#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithCellValueTruncateWithInvalidLength#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithCellValueTruncate; public WorksheetRowTypeInfo ClassWithCellValueTruncate => _ClassWithCellValueTruncate - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellValueTruncate obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellValueTruncate obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithCellValueTruncate? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithCellValueTruncate? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnHeaderForAllProperties#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnHeaderForAllProperties#MyNamespace.MyGenRowContext.g.verified.cs index 139bc426..8d41062b 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnHeaderForAllProperties#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnHeaderForAllProperties#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithColumnHeaderForAllProperties; public WorksheetRowTypeInfo ClassWithColumnHeaderForAllProperties => _ClassWithColumnHeaderForAllProperties - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -52,7 +54,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -61,12 +65,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithColumnHeaderForAllProperties obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithColumnHeaderForAllProperties obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -74,12 +81,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -87,15 +100,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithColumnHeaderForAllProperties? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithColumnHeaderForAllProperties? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -108,5 +115,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[5] = new DataCell(obj.Score); return spreadsheet.AddRowAsync(cells.AsMemory(0, 6), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnOrderForAllProperties#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnOrderForAllProperties#MyNamespace.MyGenRowContext.g.verified.cs index c711fee4..5be61b87 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnOrderForAllProperties#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnOrderForAllProperties#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithColumnOrderForAllProperties; public WorksheetRowTypeInfo ClassWithColumnOrderForAllProperties => _ClassWithColumnOrderForAllProperties - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -52,7 +54,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -61,12 +65,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnOrdering.ClassWithColumnOrderForAllProperties obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnOrdering.ClassWithColumnOrderForAllProperties obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -74,12 +81,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -87,15 +100,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnOrdering.ClassWithColumnOrderForAllProperties? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnOrdering.ClassWithColumnOrderForAllProperties? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -108,5 +115,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[5] = new DataCell(obj.Score); return spreadsheet.AddRowAsync(cells.AsMemory(0, 6), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnOrderForSomeProperties#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnOrderForSomeProperties#MyNamespace.MyGenRowContext.g.verified.cs index 506990c2..6d2d5ae2 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnOrderForSomeProperties#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnOrderForSomeProperties#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithColumnOrderForSomeProperties; public WorksheetRowTypeInfo ClassWithColumnOrderForSomeProperties => _ClassWithColumnOrderForSomeProperties - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -52,7 +54,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -61,12 +65,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnOrdering.ClassWithColumnOrderForSomeProperties obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnOrdering.ClassWithColumnOrderForSomeProperties obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -74,12 +81,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -87,15 +100,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnOrdering.ClassWithColumnOrderForSomeProperties? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnOrdering.ClassWithColumnOrderForSomeProperties? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -108,5 +115,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[5] = new DataCell(obj.Age); return spreadsheet.AddRowAsync(cells.AsMemory(0, 6), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnWidth#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnWidth#MyNamespace.MyGenRowContext.g.verified.cs index 8503796d..10acb5ba 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnWidth#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnWidth#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithColumnWidth; public WorksheetRowTypeInfo ClassWithColumnWidth => _ClassWithColumnWidth - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, CreateWorksheetOptions0); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, CreateWorksheetOptions0); private static SpreadCheetah.Worksheets.WorksheetOptions CreateWorksheetOptions0() { @@ -54,7 +56,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -63,12 +67,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithColumnWidth obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithColumnWidth obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -76,12 +83,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -89,15 +102,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithColumnWidth? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithColumnWidth? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -105,5 +112,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnWidthWithInvalidWidth#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnWidthWithInvalidWidth#MyNamespace.MyGenRowContext.g.verified.cs index dd01af98..62358e01 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnWidthWithInvalidWidth#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithColumnWidthWithInvalidWidth#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithColumnWidth; public WorksheetRowTypeInfo ClassWithColumnWidth => _ClassWithColumnWidth - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithColumnWidth obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithColumnWidth obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithColumnWidth? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithColumnWidth? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithDuplicateColumnOrdering#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithDuplicateColumnOrdering#MyNamespace.MyGenRowContext.g.verified.cs index af1f5e23..f64ff8ee 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithDuplicateColumnOrdering#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithDuplicateColumnOrdering#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithDuplicateColumnOrdering; public WorksheetRowTypeInfo ClassWithDuplicateColumnOrdering => _ClassWithDuplicateColumnOrdering - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithDuplicateColumnOrdering obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithDuplicateColumnOrdering obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithDuplicateColumnOrdering? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithDuplicateColumnOrdering? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.PropertyA); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithDuplicateColumnOrderingAcrossInheritance#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithDuplicateColumnOrderingAcrossInheritance#MyNamespace.MyGenRowContext.g.verified.cs index 4adf0a2f..6ac93e1c 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithDuplicateColumnOrderingAcrossInheritance#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithDuplicateColumnOrderingAcrossInheritance#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithDuplicateColumnOrdering; public WorksheetRowTypeInfo ClassWithDuplicateColumnOrdering => _ClassWithDuplicateColumnOrdering - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithDuplicateColumnOrdering obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithDuplicateColumnOrdering obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithDuplicateColumnOrdering? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithDuplicateColumnOrdering? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.PropertyB); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithInheritance#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithInheritance#MyNamespace.MyGenRowContext.g.verified.cs index 259ece3b..c9994b71 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithInheritance#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithInheritance#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithInheritance; public WorksheetRowTypeInfo ClassWithInheritance => _ClassWithInheritance - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -48,7 +50,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -57,12 +61,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithInheritance obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithInheritance obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(2); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -70,12 +77,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(2); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -83,15 +96,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithInheritance? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithInheritance? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -100,5 +107,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[1] = new DataCell(obj.Country); return spreadsheet.AddRowAsync(cells.AsMemory(0, 2), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithInvalidPropertyReferenceColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithInvalidPropertyReferenceColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs index df5a1b8b..a6144723 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithInvalidPropertyReferenceColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithInvalidPropertyReferenceColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithInvalidPropertyReferenceColumnHeaders; public WorksheetRowTypeInfo ClassWithInvalidPropertyReferenceColumnHeaders => _ClassWithInvalidPropertyReferenceColumnHeaders - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -53,7 +55,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -62,12 +66,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(7); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -75,12 +82,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(7); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -88,15 +101,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -110,5 +117,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[6] = new DataCell(obj.PropertyG); return spreadsheet.AddRowAsync(cells.AsMemory(0, 7), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleCellValueTruncate#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleCellValueTruncate#MyNamespace.MyGenRowContext.g.verified.cs index 7510dd8d..18b6fa9c 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleCellValueTruncate#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleCellValueTruncate#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithMultipleCellValueTruncate; public WorksheetRowTypeInfo ClassWithMultipleCellValueTruncate => _ClassWithMultipleCellValueTruncate - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -50,7 +52,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -59,12 +63,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithMultipleCellValueTruncate obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithMultipleCellValueTruncate obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(4); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -72,12 +79,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(4); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -85,27 +98,25 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithMultipleCellValueTruncate? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithMultipleCellValueTruncate? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); - var p0 = obj.FirstName; - cells[0] = p0 is null || p0.Length <= 20 ? new DataCell(p0) : new DataCell(p0.AsMemory(0, 20)); - var p1 = obj.LastName; - cells[1] = p1 is null || p1.Length <= 20 ? new DataCell(p1) : new DataCell(p1.AsMemory(0, 20)); + cells[0] = ConstructTruncatedDataCell(obj.FirstName, 20); + cells[1] = ConstructTruncatedDataCell(obj.LastName, 20); cells[2] = new DataCell(obj.YearOfBirth); - var p3 = obj.Initials; - cells[3] = p3 is null || p3.Length <= 5 ? new DataCell(p3) : new DataCell(p3.AsMemory(0, 5)); + cells[3] = ConstructTruncatedDataCell(obj.Initials, 5); return spreadsheet.AddRowAsync(cells.AsMemory(0, 4), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleColumnWidths#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleColumnWidths#MyNamespace.MyGenRowContext.g.verified.cs index 6a0b5f93..b0d47789 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleColumnWidths#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleColumnWidths#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithMultipleColumnWidths; public WorksheetRowTypeInfo ClassWithMultipleColumnWidths => _ClassWithMultipleColumnWidths - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, CreateWorksheetOptions0); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, CreateWorksheetOptions0); private static SpreadCheetah.Worksheets.WorksheetOptions CreateWorksheetOptions0() { @@ -59,7 +61,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -68,12 +72,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithMultipleColumnWidths obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithMultipleColumnWidths obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(4); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -81,12 +88,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(4); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -94,15 +107,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithMultipleColumnWidths? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.ClassWithMultipleColumnWidths? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -113,5 +120,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[3] = new DataCell(obj.Initials); return spreadsheet.AddRowAsync(cells.AsMemory(0, 4), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleProperties#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleProperties#MyNamespace.MyGenRowContext.g.verified.cs index 8262c01b..2dc33253 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleProperties#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithMultipleProperties#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithMultipleProperties; public WorksheetRowTypeInfo ClassWithMultipleProperties => _ClassWithMultipleProperties - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -52,7 +54,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -61,12 +65,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -74,12 +81,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -87,15 +100,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -108,5 +115,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[5] = new DataCell(obj.Score); return spreadsheet.AddRowAsync(cells.AsMemory(0, 6), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithNoProperties#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithNoProperties#MyNamespace.MyGenRowContext.g.verified.cs index ee04022c..b698df08 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithNoProperties#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithNoProperties#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -23,5 +24,12 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithNoProperties; public WorksheetRowTypeInfo ClassWithNoProperties => _ClassWithNoProperties ??= EmptyWorksheetRowContext.CreateTypeInfo(); + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithNoPropertiesAndWarningsSuppressed#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithNoPropertiesAndWarningsSuppressed#MyNamespace.MyGenRowContext.g.verified.cs index 76da6d7f..f1c32a66 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithNoPropertiesAndWarningsSuppressed#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithNoPropertiesAndWarningsSuppressed#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -23,5 +24,12 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithNoProperties; public WorksheetRowTypeInfo ClassWithNoProperties => _ClassWithNoProperties ??= EmptyWorksheetRowContext.CreateTypeInfo(); + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithPropertyReferenceColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithPropertyReferenceColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs index 125f0fc8..8ac95390 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithPropertyReferenceColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithPropertyReferenceColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithPropertyReferenceColumnHeaders; public WorksheetRowTypeInfo ClassWithPropertyReferenceColumnHeaders => _ClassWithPropertyReferenceColumnHeaders - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -52,7 +54,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -61,12 +65,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithPropertyReferenceColumnHeaders obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithPropertyReferenceColumnHeaders obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -74,12 +81,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -87,15 +100,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithPropertyReferenceColumnHeaders? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithPropertyReferenceColumnHeaders? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -108,5 +115,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[5] = new DataCell(obj.Age); return spreadsheet.AddRowAsync(cells.AsMemory(0, 6), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs index bb9759e7..46b79639 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithSingleProperty; public WorksheetRowTypeInfo ClassWithSingleProperty => _ClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithSpecialCharacterColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithSpecialCharacterColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs index faf28a3e..ba478ab6 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithSpecialCharacterColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithSpecialCharacterColumnHeaders#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithSpecialCharacterColumnHeaders; public WorksheetRowTypeInfo ClassWithSpecialCharacterColumnHeaders => _ClassWithSpecialCharacterColumnHeaders - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -54,7 +56,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -63,12 +67,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithSpecialCharacterColumnHeaders obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithSpecialCharacterColumnHeaders obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(8); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -76,12 +83,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(8); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -89,15 +102,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithSpecialCharacterColumnHeaders? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader.ClassWithSpecialCharacterColumnHeaders? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -112,5 +119,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[7] = new DataCell(obj.Note2); return spreadsheet.AddRowAsync(cells.AsMemory(0, 8), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithUnsupportedProperty#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithUnsupportedProperty#MyNamespace.MyGenRowContext.g.verified.cs index 4b92dd05..4af6041f 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithUnsupportedProperty#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithUnsupportedProperty#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithUnsupportedProperty; public WorksheetRowTypeInfo ClassWithUnsupportedProperty => _ClassWithUnsupportedProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithUnsupportedPropertyAndWarningsSuppressed#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithUnsupportedPropertyAndWarningsSuppressed#MyNamespace.MyGenRowContext.g.verified.cs index 5f6348a1..3eab26a9 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithUnsupportedPropertyAndWarningsSuppressed#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ClassWithUnsupportedPropertyAndWarningsSuppressed#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithUnsupportedProperty; public WorksheetRowTypeInfo ClassWithUnsupportedProperty => _ClassWithUnsupportedProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextClassWithDefaultAccessibility#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextClassWithDefaultAccessibility#MyNamespace.MyGenRowContext.g.verified.cs index 8c583e83..84581cf2 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextClassWithDefaultAccessibility#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextClassWithDefaultAccessibility#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithSingleProperty; public WorksheetRowTypeInfo ClassWithSingleProperty => _ClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextClassWithInternalAccessibility#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextClassWithInternalAccessibility#MyNamespace.MyGenRowContext.g.verified.cs index 8c583e83..84581cf2 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextClassWithInternalAccessibility#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextClassWithInternalAccessibility#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithSingleProperty; public WorksheetRowTypeInfo ClassWithSingleProperty => _ClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoClassesWithColumnWidth#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoClassesWithColumnWidth#MyNamespace.MyGenRowContext.g.verified.cs index bbf612c1..8af8bff7 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoClassesWithColumnWidth#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoClassesWithColumnWidth#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _Class1WithColumnWidth; public WorksheetRowTypeInfo Class1WithColumnWidth => _Class1WithColumnWidth - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, CreateWorksheetOptions0); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, CreateWorksheetOptions0); private static SpreadCheetah.Worksheets.WorksheetOptions CreateWorksheetOptions0() { @@ -54,7 +56,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -63,12 +67,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.Class1WithColumnWidth obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.Class1WithColumnWidth obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -76,12 +83,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -89,15 +102,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.Class1WithColumnWidth? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.Class1WithColumnWidth? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -108,7 +115,8 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee private WorksheetRowTypeInfo? _Class2WithColumnWidth; public WorksheetRowTypeInfo Class2WithColumnWidth => _Class2WithColumnWidth - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow1Async, AddAsRowAsync, AddRangeAsRowsAsync, CreateWorksheetOptions1); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow1Async, AddAsRowAsync, AddRangeAsRowsAsync, CreateWorksheetOptions1); private static SpreadCheetah.Worksheets.WorksheetOptions CreateWorksheetOptions1() { @@ -140,7 +148,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -149,12 +159,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.Class2WithColumnWidth obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.Class2WithColumnWidth obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -162,12 +175,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -175,15 +194,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.Class2WithColumnWidth? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.Class2WithColumnWidth? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -191,5 +204,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Age); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoSimilarWorksheetRowAttributes#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoSimilarWorksheetRowAttributes#MyNamespace.MyGenRowContext.g.verified.cs index bb9759e7..46b79639 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoSimilarWorksheetRowAttributes#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoSimilarWorksheetRowAttributes#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithSingleProperty; public WorksheetRowTypeInfo ClassWithSingleProperty => _ClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoWorksheetRowAttributes#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoWorksheetRowAttributes#MyNamespace.MyGenRowContext.g.verified.cs index 6c32e1b7..4d5d8512 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoWorksheetRowAttributes#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoWorksheetRowAttributes#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithSingleProperty; public WorksheetRowTypeInfo ClassWithSingleProperty => _ClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -101,7 +108,8 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee private WorksheetRowTypeInfo? _ClassWithMultipleProperties; public WorksheetRowTypeInfo ClassWithMultipleProperties => _ClassWithMultipleProperties - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow1Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow1Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow1Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -131,7 +139,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -140,12 +150,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -153,12 +166,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -166,15 +185,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -187,5 +200,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[5] = new DataCell(obj.Score); return spreadsheet.AddRowAsync(cells.AsMemory(0, 6), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoWorksheetRowAttributesWhenTheFirstTypeEmitsWarning#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoWorksheetRowAttributesWhenTheFirstTypeEmitsWarning#MyNamespace.MyGenRowContext.g.verified.cs index cf76afea..e33b8b92 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoWorksheetRowAttributesWhenTheFirstTypeEmitsWarning#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ContextWithTwoWorksheetRowAttributesWhenTheFirstTypeEmitsWarning#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithUnsupportedProperty; public WorksheetRowTypeInfo ClassWithUnsupportedProperty => _ClassWithUnsupportedProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithUnsupportedProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -101,7 +108,8 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee private WorksheetRowTypeInfo? _ClassWithSingleProperty; public WorksheetRowTypeInfo ClassWithSingleProperty => _ClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow1Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow1Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow1Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -126,7 +134,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -135,12 +145,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -148,12 +161,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -161,15 +180,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -177,5 +190,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_InternalClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_InternalClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs index fc16ff81..7a0f3422 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_InternalClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_InternalClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _InternalClassWithSingleProperty; public WorksheetRowTypeInfo InternalClassWithSingleProperty => _InternalClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, My return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.InternalClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.InternalClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.InternalClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + MyNamespace.InternalClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ReadOnlyRecordStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ReadOnlyRecordStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs index 9bfdddf7..4e253b91 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ReadOnlyRecordStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ReadOnlyRecordStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ReadOnlyRecordStructWithSingleProperty; public WorksheetRowTypeInfo ReadOnlyRecordStructWithSingleProperty => _ReadOnlyRecordStructWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -45,7 +47,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -54,12 +58,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ReadOnlyRecordStructWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ReadOnlyRecordStructWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -67,12 +74,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -80,18 +93,19 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ReadOnlyRecordStructWithSingleProperty obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } + cells[0] = new DataCell(obj.Value); + return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ReadOnlyRecordStructWithSingleProperty obj, DataCell[] cells, CancellationToken token) + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) { - cells[0] = new DataCell(obj.Value); - return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ReadOnlyStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ReadOnlyStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs index cf7b97dc..a6d04569 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ReadOnlyStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_ReadOnlyStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ReadOnlyStructWithSingleProperty; public WorksheetRowTypeInfo ReadOnlyStructWithSingleProperty => _ReadOnlyStructWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -45,7 +47,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -54,12 +58,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ReadOnlyStructWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ReadOnlyStructWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -67,12 +74,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -80,18 +93,19 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ReadOnlyStructWithSingleProperty obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } + cells[0] = new DataCell(obj.Value); + return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ReadOnlyStructWithSingleProperty obj, DataCell[] cells, CancellationToken token) + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) { - cells[0] = new DataCell(obj.Value); - return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_InheritedColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_InheritedColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs index 23cd98c8..47c5fa44 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_InheritedColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_InheritedColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClassWithInheritedColumnsFirst; public WorksheetRowTypeInfo RecordClassWithInheritedColumnsFirst => _RecordClassWithInheritedColumnsFirst - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -48,7 +50,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -57,12 +61,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithInheritedColumnsFirst obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithInheritedColumnsFirst obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(2); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -70,12 +77,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(2); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -83,15 +96,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithInheritedColumnsFirst? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithInheritedColumnsFirst? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -100,5 +107,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[1] = new DataCell(obj.ClassProperty); return spreadsheet.AddRowAsync(cells.AsMemory(0, 2), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_InheritedColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_InheritedColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs index 1b4bb641..6c1d90aa 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_InheritedColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_InheritedColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClassWithInheritedColumnsLast; public WorksheetRowTypeInfo RecordClassWithInheritedColumnsLast => _RecordClassWithInheritedColumnsLast - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -48,7 +50,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -57,12 +61,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithInheritedColumnsLast obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithInheritedColumnsLast obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(2); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -70,12 +77,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(2); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -83,15 +96,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithInheritedColumnsLast? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithInheritedColumnsLast? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -100,5 +107,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[1] = new DataCell(obj.Value); return spreadsheet.AddRowAsync(cells.AsMemory(0, 2), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_WithoutInheritanceAttribute#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_WithoutInheritanceAttribute#MyNamespace.MyGenRowContext.g.verified.cs index d6da3dc6..f301a6d0 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_WithoutInheritanceAttribute#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithInheritance_WithoutInheritanceAttribute#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClassWithIgnoreInheritance; public WorksheetRowTypeInfo RecordClassWithIgnoreInheritance => _RecordClassWithIgnoreInheritance - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -48,7 +50,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -57,12 +61,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithIgnoreInheritance obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithIgnoreInheritance obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(2); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -70,12 +77,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(2); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -83,15 +96,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithIgnoreInheritance? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClassWithIgnoreInheritance? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -100,5 +107,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[1] = new DataCell(obj.Age); return spreadsheet.AddRowAsync(cells.AsMemory(0, 2), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs index 5fe640b4..2f8d3bcd 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClassWithSingleProperty; public WorksheetRowTypeInfo RecordClassWithSingleProperty => _RecordClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.RecordClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.RecordClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.RecordClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.RecordClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Value); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs index 34d49c0d..8a20cd7d 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClass2LevelOfInheritanceInheritedColumnsFirst; public WorksheetRowTypeInfo RecordClass2LevelOfInheritanceInheritedColumnsFirst => _RecordClass2LevelOfInheritanceInheritedColumnsFirst - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -49,7 +51,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -58,12 +62,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirst obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirst obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -71,12 +78,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -84,15 +97,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirst? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirst? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -102,5 +109,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[2] = new DataCell(obj.OwnProperty); return spreadsheet.AddRowAsync(cells.AsMemory(0, 3), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsFirst_ParentIgnore_Inheritance#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsFirst_ParentIgnore_Inheritance#MyNamespace.MyGenRowContext.g.verified.cs index a711820f..6952ad1b 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsFirst_ParentIgnore_Inheritance#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsFirst_ParentIgnore_Inheritance#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClass2LevelOfInheritanceInheritedColumnsFirstParentIgnoreInheritance; public WorksheetRowTypeInfo RecordClass2LevelOfInheritanceInheritedColumnsFirstParentIgnoreInheritance => _RecordClass2LevelOfInheritanceInheritedColumnsFirstParentIgnoreInheritance - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -49,7 +51,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -58,12 +62,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirstParentIgnoreInheritance obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirstParentIgnoreInheritance obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -71,12 +78,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -84,15 +97,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirstParentIgnoreInheritance? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirstParentIgnoreInheritance? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -102,5 +109,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[2] = new DataCell(obj.ClassProperty); return spreadsheet.AddRowAsync(cells.AsMemory(0, 3), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs index d35326bf..ae4aebd2 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClass2LevelOfInheritanceInheritedColumnsLast; public WorksheetRowTypeInfo RecordClass2LevelOfInheritanceInheritedColumnsLast => _RecordClass2LevelOfInheritanceInheritedColumnsLast - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -49,7 +51,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -58,12 +62,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLast obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLast obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -71,12 +78,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -84,15 +97,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLast? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLast? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -102,5 +109,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[2] = new DataCell(obj.ClassProperty); return spreadsheet.AddRowAsync(cells.AsMemory(0, 3), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentIgnore_Inheritance#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentIgnore_Inheritance#MyNamespace.MyGenRowContext.g.verified.cs index 4a2a2043..08ad549b 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentIgnore_Inheritance#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentIgnore_Inheritance#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClass2LevelOfInheritanceInheritedColumnsLastParentIgnoreInheritance; public WorksheetRowTypeInfo RecordClass2LevelOfInheritanceInheritedColumnsLastParentIgnoreInheritance => _RecordClass2LevelOfInheritanceInheritedColumnsLastParentIgnoreInheritance - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -49,7 +51,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -58,12 +62,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLastParentIgnoreInheritance obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLastParentIgnoreInheritance obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -71,12 +78,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -84,15 +97,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLastParentIgnoreInheritance? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLastParentIgnoreInheritance? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -102,5 +109,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[2] = new DataCell(obj.Age); return spreadsheet.AddRowAsync(cells.AsMemory(0, 3), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentInheritColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentInheritColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs index c580f51f..ebd4b605 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentInheritColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentInheritColumnsFirst#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClass2LevelOfInheritanceInheritedColumnsLastParentInheritColumnsFirst; public WorksheetRowTypeInfo RecordClass2LevelOfInheritanceInheritedColumnsLastParentInheritColumnsFirst => _RecordClass2LevelOfInheritanceInheritedColumnsLastParentInheritColumnsFirst - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -49,7 +51,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -58,12 +62,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLastParentInheritColumnsFirst obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLastParentInheritColumnsFirst obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -71,12 +78,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -84,15 +97,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLastParentInheritColumnsFirst? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsLastParentInheritColumnsFirst? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -102,5 +109,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[2] = new DataCell(obj.Value); return spreadsheet.AddRowAsync(cells.AsMemory(0, 3), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentInheritColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentInheritColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs index 9174d1e8..28e5c680 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentInheritColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordClassWith_2LevelOfInheritance_InheritedColumnsLast_ParentInheritColumnsLast#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordClass2LevelOfInheritanceInheritedColumnsFirstParentInheritColumnsLast; public WorksheetRowTypeInfo RecordClass2LevelOfInheritanceInheritedColumnsFirstParentInheritColumnsLast => _RecordClass2LevelOfInheritanceInheritedColumnsFirstParentInheritColumnsLast - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -49,7 +51,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -58,12 +62,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirstParentInheritColumnsLast obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirstParentInheritColumnsLast obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -71,12 +78,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(3); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -84,15 +97,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirstParentInheritColumnsLast? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.InheritColumns.RecordClass2LevelOfInheritanceInheritedColumnsFirstParentInheritColumnsLast? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -102,5 +109,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[2] = new DataCell(obj.OwnValue); return spreadsheet.AddRowAsync(cells.AsMemory(0, 3), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs index 0f220187..698b7e5e 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_RecordStructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _RecordStructWithSingleProperty; public WorksheetRowTypeInfo RecordStructWithSingleProperty => _RecordStructWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -45,7 +47,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -54,12 +58,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.RecordStructWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.RecordStructWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -67,12 +74,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -80,18 +93,19 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.RecordStructWithSingleProperty obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } + cells[0] = new DataCell(obj.Value); + return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.RecordStructWithSingleProperty obj, DataCell[] cells, CancellationToken token) + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) { - cells[0] = new DataCell(obj.Value); - return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_StructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_StructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs index 65bcdbbb..12bd74e0 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_StructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_StructWithSingleProperty#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _StructWithSingleProperty; public WorksheetRowTypeInfo StructWithSingleProperty => _StructWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -45,7 +47,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -54,12 +58,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.StructWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.StructWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -67,12 +74,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -80,18 +93,19 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.StructWithSingleProperty obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } + cells[0] = new DataCell(obj.Id); + return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.StructWithSingleProperty obj, DataCell[] cells, CancellationToken token) + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) { - cells[0] = new DataCell(obj.Id); - return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_TwoContextClasses#MyNamespace.MyGenRowContext.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_TwoContextClasses#MyNamespace.MyGenRowContext.g.verified.cs index 73246c34..5a902b08 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_TwoContextClasses#MyNamespace.MyGenRowContext.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_TwoContextClasses#MyNamespace.MyGenRowContext.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext() private WorksheetRowTypeInfo? _ClassWithSingleProperty; public WorksheetRowTypeInfo ClassWithSingleProperty => _ClassWithSingleProperty - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -47,7 +49,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -56,12 +60,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -69,12 +76,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(1); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -82,15 +95,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithSingleProperty? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -98,5 +105,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[0] = new DataCell(obj.Name); return spreadsheet.AddRowAsync(cells.AsMemory(0, 1), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_TwoContextClasses#MyNamespace.MyGenRowContext2.g.verified.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_TwoContextClasses#MyNamespace.MyGenRowContext2.g.verified.cs index d7d61b27..15e1f152 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_TwoContextClasses#MyNamespace.MyGenRowContext2.g.verified.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Snapshots/G.WorksheetRowGenerator_Generate_TwoContextClasses#MyNamespace.MyGenRowContext2.g.verified.cs @@ -3,6 +3,7 @@ #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; +using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -22,7 +23,8 @@ public MyGenRowContext2() private WorksheetRowTypeInfo? _ClassWithMultipleProperties; public WorksheetRowTypeInfo ClassWithMultipleProperties => _ClassWithMultipleProperties - ??= WorksheetRowMetadataServices.CreateObjectInfo(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync); + ??= WorksheetRowMetadataServices.CreateObjectInfo( + AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync, null); private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token) { @@ -52,7 +54,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, Sp return AddAsRowInternalAsync(spreadsheet, obj, token); } - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -61,12 +65,15 @@ private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadshe return AddRangeAsRowsInternalAsync(spreadsheet, objs, token); } - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties obj, CancellationToken token) + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties obj, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); } finally { @@ -74,12 +81,18 @@ private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet s } } - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, CancellationToken token) + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable objs, + CancellationToken token) { var cells = ArrayPool.Shared.Rent(6); try { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); + var styleIds = Array.Empty(); + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } } finally { @@ -87,15 +100,9 @@ private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreads } } - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - } - - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties? obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + SpreadCheetah.SourceGenerator.SnapshotTest.Models.ClassWithMultipleProperties? obj, + DataCell[] cells, IReadOnlyList styleIds, CancellationToken token) { if (obj is null) return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); @@ -108,5 +115,12 @@ private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadshee cells[5] = new DataCell(obj.Score); return spreadsheet.AddRowAsync(cells.AsMemory(0, 6), token); } + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/CellStyleTests.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/CellStyleTests.cs new file mode 100644 index 00000000..fed675ef --- /dev/null +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/CellStyleTests.cs @@ -0,0 +1,102 @@ +using SpreadCheetah.SourceGenerator.SnapshotTest.Helpers; +using SpreadCheetah.SourceGenerators; + +namespace SpreadCheetah.SourceGenerator.SnapshotTest.Tests; + +public class CellStyleTests +{ + [Fact] + public Task CellStyle_ClassWithMultipleAttributes() + { + // Arrange + const string source = """ + using SpreadCheetah.SourceGeneration; + + namespace MyNamespace; + + public class ClassWithCellStyle + { + [CellStyle("Italic")] + public string? FirstName { get; set; } + + [CellStyle("Bold")] + public string? LastName { get; set; } + + public int YearOfBirth { get; set; } + + [CellStyle("Bold")] + public string? Initials { get; set; } + } + + [WorksheetRow(typeof(ClassWithCellStyle))] + public partial class MyGenRowContext : WorksheetRowContext; + """; + + // Act & Assert + return TestHelper.CompileAndVerify(source); + } + + [Fact] + public Task CellStyle_ContextWithTwoClassesWithCellStyle() + { + // Arrange + const string source = """ + using SpreadCheetah.SourceGeneration; + + namespace MyNamespace; + + public class ClassWithCellStyle + { + [CellStyle("Italic")] + public string? FirstName { get; set; } + + [CellStyle("Bold")] + public string? LastName { get; set; } + + public int YearOfBirth { get; set; } + + [CellStyle("Bold")] + public string? Initials { get; set; } + } + + public class Class2WithCellStyle + { + [CellStyle("Red")] + public string? Id { get; set; } + + [CellStyle("Bold")] + public decimal Price { get; set; } + } + + [WorksheetRow(typeof(ClassWithCellStyle))] + [WorksheetRow(typeof(Class2WithCellStyle))] + public partial class MyGenRowContext : WorksheetRowContext; + """; + + // Act & Assert + return TestHelper.CompileAndVerify(source); + } + + [Fact] + public Task CellStyle_ClassWithEmptyCellStyle() + { + // Arrange + const string source = """ + using SpreadCheetah.SourceGeneration; + + namespace MyNamespace; + + public class ClassWithCellStyle + { + [CellStyle("")] + public string? FirstName { get; set; } + } + + [WorksheetRow(typeof(ClassWithCellStyle))] + public partial class MyGenRowContext : WorksheetRowContext; + """; + + // Act & Assert + return TestHelper.CompileAndVerify(source, onlyDiagnostics: true); + } +} diff --git a/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/CellStyleContext.cs b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/CellStyleContext.cs new file mode 100644 index 00000000..ec2a0261 --- /dev/null +++ b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/CellStyleContext.cs @@ -0,0 +1,10 @@ +using SpreadCheetah.SourceGeneration; + +namespace SpreadCheetah.SourceGenerator.Test.Models.CellStyle; + +[WorksheetRow(typeof(ClassWithCellStyle))] +[WorksheetRow(typeof(ClassWithCellStyleOnDateTimeProperty))] +[WorksheetRow(typeof(ClassWithCellStyleOnTruncatedProperty))] +[WorksheetRow(typeof(ClassWithMultipleCellStyles))] +[WorksheetRow(typeof(RecordWithCellStyle))] +public partial class CellStyleContext : WorksheetRowContext; \ No newline at end of file diff --git a/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyle.cs b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyle.cs new file mode 100644 index 00000000..a508afa6 --- /dev/null +++ b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyle.cs @@ -0,0 +1,9 @@ +using SpreadCheetah.SourceGeneration; + +namespace SpreadCheetah.SourceGenerator.Test.Models.CellStyle; + +public class ClassWithCellStyle +{ + [CellStyle("Price style")] + public decimal Price { get; set; } +} diff --git a/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyleOnDateTimeProperty.cs b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyleOnDateTimeProperty.cs new file mode 100644 index 00000000..0450ffe5 --- /dev/null +++ b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyleOnDateTimeProperty.cs @@ -0,0 +1,9 @@ +using SpreadCheetah.SourceGeneration; + +namespace SpreadCheetah.SourceGenerator.Test.Models.CellStyle; + +public class ClassWithCellStyleOnDateTimeProperty +{ + [CellStyle("Created style")] + public DateTime CreatedDate { get; set; } +} diff --git a/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyleOnTruncatedProperty.cs b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyleOnTruncatedProperty.cs new file mode 100644 index 00000000..25e325a5 --- /dev/null +++ b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithCellStyleOnTruncatedProperty.cs @@ -0,0 +1,10 @@ +using SpreadCheetah.SourceGeneration; + +namespace SpreadCheetah.SourceGenerator.Test.Models.CellStyle; + +public class ClassWithCellStyleOnTruncatedProperty +{ + [CellStyle("Name style")] + [CellValueTruncate(10)] + public required string Name { get; init; } +} diff --git a/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithMultipleCellStyles.cs b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithMultipleCellStyles.cs new file mode 100644 index 00000000..0c3deee3 --- /dev/null +++ b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/ClassWithMultipleCellStyles.cs @@ -0,0 +1,17 @@ +using SpreadCheetah.SourceGeneration; + +namespace SpreadCheetah.SourceGenerator.Test.Models.CellStyle; + +public class ClassWithMultipleCellStyles +{ + [CellStyle("Name")] + public required string FirstName { get; set; } + + public string? MiddleName { get; set; } + + [CellStyle("Name")] + public required string LastName { get; set; } + + [CellStyle("Age")] + public required int Age { get; set; } +} \ No newline at end of file diff --git a/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/RecordWithCellStyle.cs b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/RecordWithCellStyle.cs new file mode 100644 index 00000000..a8f21c1d --- /dev/null +++ b/SpreadCheetah.SourceGenerator.Test/Models/CellStyle/RecordWithCellStyle.cs @@ -0,0 +1,9 @@ +using SpreadCheetah.SourceGeneration; + +namespace SpreadCheetah.SourceGenerator.Test.Models.CellStyle; + +public record RecordWithCellStyle +{ + [CellStyle("Name style")] + public required string Name { get; init; } +} diff --git a/SpreadCheetah.SourceGenerator.Test/Models/Combinations/ClassWithColumnAttributes.cs b/SpreadCheetah.SourceGenerator.Test/Models/Combinations/ClassWithColumnAttributes.cs index 9812c9bd..b58b7202 100644 --- a/SpreadCheetah.SourceGenerator.Test/Models/Combinations/ClassWithColumnAttributes.cs +++ b/SpreadCheetah.SourceGenerator.Test/Models/Combinations/ClassWithColumnAttributes.cs @@ -16,6 +16,7 @@ public class ClassWithColumnAttributes(string id, string countryOfOrigin, string [ColumnOrder(1)] [ColumnWidth(10)] + [CellStyle("Year style")] public int Year { get; } = year; #pragma warning disable IDE1006 // Naming Styles diff --git a/SpreadCheetah.SourceGenerator.Test/Tests/CellStyleTests.cs b/SpreadCheetah.SourceGenerator.Test/Tests/CellStyleTests.cs new file mode 100644 index 00000000..335e737c --- /dev/null +++ b/SpreadCheetah.SourceGenerator.Test/Tests/CellStyleTests.cs @@ -0,0 +1,215 @@ +using SpreadCheetah.SourceGenerator.Test.Models.CellStyle; +using SpreadCheetah.Styling; +using SpreadCheetah.TestHelpers.Assertions; +using Xunit; + +namespace SpreadCheetah.SourceGenerator.Test.Tests; + +public class CellStyleTests +{ + [Fact] + public async Task CellStyle_ClassWithSingleAttribute() + { + // Arrange + using var stream = new MemoryStream(); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); + var style = new Style { Font = { Bold = true } }; + spreadsheet.AddStyle(style, "Price style"); + var obj = new ClassWithCellStyle { Price = 199.90m }; + + // Act + await spreadsheet.AddAsRowAsync(obj, CellStyleContext.Default.ClassWithCellStyle); + await spreadsheet.FinishAsync(); + + // Assert + using var sheet = SpreadsheetAssert.SingleSheet(stream); + Assert.Equal(obj.Price, sheet["A1"].DecimalValue); + Assert.True(sheet["A1"].Style.Font.Bold); + } + + [Fact] + public async Task CellStyle_ClassWithMultipleAttributes() + { + // Arrange + using var stream = new MemoryStream(); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); + var boldStyle = new Style { Font = { Bold = true } }; + spreadsheet.AddStyle(boldStyle, "Name"); + var italicStyle = new Style { Font = { Italic = true } }; + spreadsheet.AddStyle(italicStyle, "Age"); + var obj = new ClassWithMultipleCellStyles + { + FirstName = "Ola", + MiddleName = "Jonsen", + LastName = "Nordmann", + Age = 42 + }; + + // Act + await spreadsheet.AddAsRowAsync(obj, CellStyleContext.Default.ClassWithMultipleCellStyles); + await spreadsheet.FinishAsync(); + + // Assert + using var sheet = SpreadsheetAssert.SingleSheet(stream); + Assert.True(sheet["A1"].Style.Font.Bold); + Assert.False(sheet["A1"].Style.Font.Italic); + Assert.False(sheet["B1"].Style.Font.Bold); + Assert.False(sheet["B1"].Style.Font.Italic); + Assert.True(sheet["C1"].Style.Font.Bold); + Assert.False(sheet["C1"].Style.Font.Italic); + Assert.False(sheet["D1"].Style.Font.Bold); + Assert.True(sheet["D1"].Style.Font.Italic); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task CellStyle_ClassWithAttributeOnDateTimeProperty(bool withDefaultDateTimeFormat) + { + // Arrange + using var stream = new MemoryStream(); + var options = withDefaultDateTimeFormat + ? new SpreadCheetahOptions() + : new SpreadCheetahOptions { DefaultDateTimeFormat = null }; + + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream, options); + await spreadsheet.StartWorksheetAsync("Sheet"); + var style = new Style { Font = { Bold = true } }; + spreadsheet.AddStyle(style, "Created style"); + var obj = new ClassWithCellStyleOnDateTimeProperty { CreatedDate = new DateTime(2022, 10, 23, 15, 16, 17, DateTimeKind.Utc) }; + + // Act + await spreadsheet.AddAsRowAsync(obj, CellStyleContext.Default.ClassWithCellStyleOnDateTimeProperty); + await spreadsheet.FinishAsync(); + + // Assert + using var sheet = SpreadsheetAssert.SingleSheet(stream); + Assert.Equal(obj.CreatedDate, sheet["A1"].DateTimeValue); + var actualStyle = sheet["A1"].Style; + Assert.True(actualStyle.Font.Bold); + Assert.Equal(withDefaultDateTimeFormat, actualStyle.NumberFormat.Format is not null); + } + + [Fact] + public async Task CellStyle_ClassWithAttributeOnTruncatedProperty() + { + // Arrange + using var stream = new MemoryStream(); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); + var style = new Style { Font = { Bold = true } }; + spreadsheet.AddStyle(style, "Name style"); + var obj = new ClassWithCellStyleOnTruncatedProperty { Name = "Ola Nordmann" }; + + // Act + await spreadsheet.AddAsRowAsync(obj, CellStyleContext.Default.ClassWithCellStyleOnTruncatedProperty); + await spreadsheet.FinishAsync(); + + // Assert + using var sheet = SpreadsheetAssert.SingleSheet(stream); + Assert.Equal(obj.Name[..10], sheet["A1"].StringValue); + Assert.True(sheet["A1"].Style.Font.Bold); + } + + [Fact] + public async Task CellStyle_MultipleTypesWithAttributeInSameWorksheet() + { + // Arrange + using var stream = new MemoryStream(); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); + var nameStyle = new Style { Font = { Italic = true } }; + var priceStyle = new Style { Font = { Bold = true } }; + spreadsheet.AddStyle(nameStyle, "Name style"); + spreadsheet.AddStyle(priceStyle, "Price style"); + var obj1 = new ClassWithCellStyle { Price = 199.90m }; + var obj2 = new RecordWithCellStyle { Name = "Tom" }; + + // Act + await spreadsheet.AddAsRowAsync(obj1, CellStyleContext.Default.ClassWithCellStyle); + await spreadsheet.AddAsRowAsync(obj2, CellStyleContext.Default.RecordWithCellStyle); + await spreadsheet.FinishAsync(); + + // Assert + using var sheet = SpreadsheetAssert.SingleSheet(stream); + Assert.Equal(obj1.Price, sheet["A1"].DecimalValue); + Assert.Equal(obj2.Name, sheet["A2"].StringValue); + Assert.True(sheet["A1"].Style.Font.Bold); + Assert.True(sheet["A2"].Style.Font.Italic); + } + + [Fact] + public async Task CellStyle_MultipleTypesWithAttributeInMultipleWorksheets() + { + // Arrange + using var stream = new MemoryStream(); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + + // Act + await spreadsheet.StartWorksheetAsync("Sheet 1"); + var priceStyle = new Style { Font = { Bold = true } }; + spreadsheet.AddStyle(priceStyle, "Price style"); + var obj1 = new ClassWithCellStyle { Price = 199.90m }; + await spreadsheet.AddAsRowAsync(obj1, CellStyleContext.Default.ClassWithCellStyle); + + await spreadsheet.StartWorksheetAsync("Sheet 2"); + var nameStyle = new Style { Font = { Italic = true } }; + spreadsheet.AddStyle(nameStyle, "Name style"); + var obj2 = new RecordWithCellStyle { Name = "Tom" }; + await spreadsheet.AddAsRowAsync(obj2, CellStyleContext.Default.RecordWithCellStyle); + + await spreadsheet.FinishAsync(); + + // Assert + using var sheets = SpreadsheetAssert.Sheets(stream); + Assert.Equal(2, sheets.Count); + + var sheet1 = sheets[0]; + Assert.Equal(obj1.Price, sheet1["A1"].DecimalValue); + Assert.True(sheet1["A1"].Style.Font.Bold); + + var sheet2 = sheets[1]; + Assert.Equal(obj2.Name, sheet2["A1"].StringValue); + Assert.True(sheet2["A1"].Style.Font.Italic); + } + + [Fact] + public async Task CellStyle_ClassWithMissingStyleName() + { + // Arrange + await using var spreadsheet = await Spreadsheet.CreateNewAsync(Stream.Null); + await spreadsheet.StartWorksheetAsync("Sheet"); + var obj = new ClassWithCellStyle { Price = 199.90m }; + + // Act + var exception = await Record.ExceptionAsync(async () => await spreadsheet.AddAsRowAsync(obj, CellStyleContext.Default.ClassWithCellStyle)); + + // Assert + var actual = Assert.IsType(exception); + Assert.Contains(nameof(Spreadsheet.AddStyle), actual.Message, StringComparison.Ordinal); + } + + [Fact] + public async Task CellStyle_WorksheetRowDependencyInfoCreatedOnlyOnce() + { + // Arrange + await using var spreadsheet = await Spreadsheet.CreateNewAsync(Stream.Null); + await spreadsheet.StartWorksheetAsync("Sheet"); + var style = new Style { Font = { Bold = true } }; + spreadsheet.AddStyle(style, "Price style"); + var typeInfo = CellStyleContext.Default.ClassWithCellStyle; + var initialDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(typeInfo); + + // Act + await spreadsheet.AddAsRowAsync(new ClassWithCellStyle { Price = 10m }, typeInfo); + await spreadsheet.AddAsRowAsync(new ClassWithCellStyle { Price = 20m }, typeInfo); + await spreadsheet.AddAsRowAsync(new ClassWithCellStyle { Price = 30m }, typeInfo); + await spreadsheet.FinishAsync(); + + // Assert + var subsequentDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(typeInfo); + Assert.Same(initialDependencyInfo, subsequentDependencyInfo); + } +} \ No newline at end of file diff --git a/SpreadCheetah.SourceGenerator.Test/Tests/WorksheetRowGeneratorTests.cs b/SpreadCheetah.SourceGenerator.Test/Tests/WorksheetRowGeneratorTests.cs index 1b8cd271..3e9df4ca 100644 --- a/SpreadCheetah.SourceGenerator.Test/Tests/WorksheetRowGeneratorTests.cs +++ b/SpreadCheetah.SourceGenerator.Test/Tests/WorksheetRowGeneratorTests.cs @@ -18,6 +18,7 @@ using Xunit; using OpenXmlCell = DocumentFormat.OpenXml.Spreadsheet.Cell; + #if NET472 using SpreadCheetah.SourceGenerator.Test.Helpers.Backporting; #endif @@ -754,6 +755,7 @@ public async Task Spreadsheet_AddAsRow_ObjectWithMultipleColumnAttributes() using var stream = new MemoryStream(); await using var s = await Spreadsheet.CreateNewAsync(stream); await s.StartWorksheetAsync("Sheet", ctx); + s.AddStyle(new Style { Font = { Bold = true } }, "Year style"); var obj = new ClassWithColumnAttributes( id: Guid.NewGuid().ToString(), @@ -778,6 +780,8 @@ public async Task Spreadsheet_AddAsRow_ObjectWithMultipleColumnAttributes() Assert.Equal(obj.Length, sheet["F1"].DecimalValue); Assert.Equal(obj.Id, sheet["G1"].StringValue); + Assert.True(sheet["A1"].Style.Font.Bold); + Assert.Equal(10, sheet.Column("A").Width, 4); Assert.Equal(40, sheet.Column("C").Width, 4); } diff --git a/SpreadCheetah.SourceGenerator/Extensions/AttributeDataExtensions.cs b/SpreadCheetah.SourceGenerator/Extensions/AttributeDataExtensions.cs index e98d39bb..b03facbb 100644 --- a/SpreadCheetah.SourceGenerator/Extensions/AttributeDataExtensions.cs +++ b/SpreadCheetah.SourceGenerator/Extensions/AttributeDataExtensions.cs @@ -2,6 +2,7 @@ using Microsoft.CodeAnalysis.CSharp; using SpreadCheetah.SourceGenerator.Helpers; using SpreadCheetah.SourceGenerator.Models; +using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Globalization; @@ -9,6 +10,34 @@ namespace SpreadCheetah.SourceGenerator.Extensions; internal static class AttributeDataExtensions { + public static PropertyAttributeData MapToPropertyAttributeData( + this ImmutableArray attributes, + ITypeSymbol propertyType, + List diagnosticInfos, + CancellationToken token) + { + var result = new PropertyAttributeData(); + + foreach (var attribute in attributes) + { + var displayString = attribute.AttributeClass?.ToDisplayString(); + if (displayString is null) + continue; + + _ = displayString switch + { + Attributes.CellStyle => attribute.TryGetCellStyleAttribute(diagnosticInfos, token, ref result), + Attributes.CellValueTruncate => attribute.TryGetCellValueTruncateAttribute(propertyType, diagnosticInfos, token, ref result), + Attributes.ColumnHeader => attribute.TryGetColumnHeaderAttribute(diagnosticInfos, token, ref result), + Attributes.ColumnOrder => attribute.TryGetColumnOrderAttribute(token, ref result), + Attributes.ColumnWidth => attribute.TryGetColumnWidthAttribute(diagnosticInfos, token, ref result), + _ => false + }; + } + + return result; + } + public static bool TryParseWorksheetRowAttribute( this AttributeData attribute, CancellationToken token, @@ -72,24 +101,24 @@ public static bool TryParseOptionsAttribute( return InheritedColumnOrder.InheritedColumnsFirst; } - public static ColumnHeader? TryGetColumnHeaderAttribute(this AttributeData attribute, ICollection diagnosticInfos, CancellationToken token) + private static bool TryGetColumnHeaderAttribute(this AttributeData attribute, + List diagnosticInfos, CancellationToken token, ref PropertyAttributeData result) { - if (!string.Equals(Attributes.ColumnHeader, attribute.AttributeClass?.ToDisplayString(), StringComparison.Ordinal)) - return null; + if (result.ColumnHeader is not null) + return false; var args = attribute.ConstructorArguments; if (args is [{ Value: string } arg]) - return new ColumnHeader(arg.ToCSharpString()); - - if (args is [{ Value: INamedTypeSymbol type }, { Value: string propertyName }]) - return TryGetColumnHeaderWithPropertyReference(type, propertyName, attribute, diagnosticInfos, token); + result.ColumnHeader = new ColumnHeader(arg.ToCSharpString()); + else if (args is [{ Value: INamedTypeSymbol type }, { Value: string propertyName }]) + result.ColumnHeader = TryGetColumnHeaderWithPropertyReference(type, propertyName, attribute, diagnosticInfos, token); - return null; + return result.ColumnHeader is not null; } private static ColumnHeader? TryGetColumnHeaderWithPropertyReference( INamedTypeSymbol type, string propertyName, AttributeData attribute, - ICollection diagnosticInfos, CancellationToken token) + List diagnosticInfos, CancellationToken token) { var typeFullName = type.ToDisplayString(); @@ -113,67 +142,94 @@ public static bool TryParseOptionsAttribute( return null; } - public static ColumnOrder? TryGetColumnOrderAttribute(this AttributeData attribute, CancellationToken token) + private static bool TryGetColumnOrderAttribute(this AttributeData attribute, CancellationToken token, ref PropertyAttributeData result) { - if (!string.Equals(Attributes.ColumnOrder, attribute.AttributeClass?.ToDisplayString(), StringComparison.Ordinal)) - return null; + if (result.ColumnOrder is not null) + return false; var args = attribute.ConstructorArguments; if (args is not [{ Value: int attributeValue }]) - return null; + return false; var location = attribute.GetLocation(token); - return new ColumnOrder(attributeValue, location); + result.ColumnOrder = new ColumnOrder(attributeValue, location); + return true; } - public static ColumnWidth? TryGetColumnWidthAttribute(this AttributeData attribute, - ICollection diagnosticInfos, CancellationToken token) + private static bool TryGetCellStyleAttribute(this AttributeData attribute, + List diagnosticInfos, CancellationToken token, ref PropertyAttributeData result) { - if (!string.Equals(Attributes.ColumnWidth, attribute.AttributeClass?.ToDisplayString(), StringComparison.Ordinal)) - return null; + if (result.CellStyle is not null) + return false; + + var args = attribute.ConstructorArguments; + if (args is not [{ Value: string value } arg]) + return false; + + if (string.IsNullOrEmpty(value) + || value.Length > 255 + || char.IsWhiteSpace(value[0]) + || char.IsWhiteSpace(value[^1])) + { + var location = attribute.GetLocation(token); + diagnosticInfos.Add(new DiagnosticInfo(Diagnostics.InvalidAttributeArgument, location, new([value, Attributes.CellStyle]))); + return false; + } + + result.CellStyle = new CellStyle(arg.ToCSharpString()); + return true; + } + + private static bool TryGetColumnWidthAttribute(this AttributeData attribute, + List diagnosticInfos, CancellationToken token, ref PropertyAttributeData result) + { + if (result.ColumnWidth is not null) + return false; var args = attribute.ConstructorArguments; if (args is not [{ Value: double attributeValue }]) - return null; + return false; if (attributeValue is <= 0 or > 255) { var location = attribute.GetLocation(token); var stringValue = attributeValue.ToString(CultureInfo.InvariantCulture); diagnosticInfos.Add(new DiagnosticInfo(Diagnostics.InvalidAttributeArgument, location, new([stringValue, Attributes.ColumnWidth]))); - return null; + return false; } - return new ColumnWidth(attributeValue); + result.ColumnWidth = new ColumnWidth(attributeValue); + return true; } - public static CellValueTruncate? TryGetCellValueTruncateAttribute(this AttributeData attribute, ITypeSymbol propertyType, - ICollection diagnosticInfos, CancellationToken token) + private static bool TryGetCellValueTruncateAttribute(this AttributeData attribute, ITypeSymbol propertyType, + List diagnosticInfos, CancellationToken token, ref PropertyAttributeData data) { - if (!string.Equals(Attributes.CellValueTruncate, attribute.AttributeClass?.ToDisplayString(), StringComparison.Ordinal)) - return null; + if (data.CellValueTruncate is not null) + return false; if (propertyType.SpecialType != SpecialType.System_String) { var location = attribute.GetLocation(token); var typeFullName = propertyType.ToDisplayString(); diagnosticInfos.Add(new DiagnosticInfo(Diagnostics.UnsupportedTypeForCellValueLengthLimit, location, new([typeFullName]))); - return null; + return false; } var args = attribute.ConstructorArguments; if (args is not [{ Value: int attributeValue }]) - return null; + return false; if (attributeValue <= 0) { var location = attribute.GetLocation(token); var stringValue = attributeValue.ToString(CultureInfo.InvariantCulture); diagnosticInfos.Add(new DiagnosticInfo(Diagnostics.InvalidAttributeArgument, location, new([stringValue, Attributes.CellValueTruncate]))); - return null; + return false; } - return new CellValueTruncate(attributeValue); + data.CellValueTruncate = new CellValueTruncate(attributeValue); + return true; } private static LocationInfo? GetLocation(this AttributeData attribute, CancellationToken token) diff --git a/SpreadCheetah.SourceGenerator/Helpers/Attributes.cs b/SpreadCheetah.SourceGenerator/Helpers/Attributes.cs index c454dcdb..18d03747 100644 --- a/SpreadCheetah.SourceGenerator/Helpers/Attributes.cs +++ b/SpreadCheetah.SourceGenerator/Helpers/Attributes.cs @@ -2,6 +2,7 @@ namespace SpreadCheetah.SourceGenerator.Helpers; internal static class Attributes { + public const string CellStyle = "SpreadCheetah.SourceGeneration.CellStyleAttribute"; public const string CellValueTruncate = "SpreadCheetah.SourceGeneration.CellValueTruncateAttribute"; public const string ColumnHeader = "SpreadCheetah.SourceGeneration.ColumnHeaderAttribute"; public const string ColumnOrder = "SpreadCheetah.SourceGeneration.ColumnOrderAttribute"; diff --git a/SpreadCheetah.SourceGenerator/Models/CellStyle.cs b/SpreadCheetah.SourceGenerator/Models/CellStyle.cs new file mode 100644 index 00000000..f86a75a2 --- /dev/null +++ b/SpreadCheetah.SourceGenerator/Models/CellStyle.cs @@ -0,0 +1,3 @@ +namespace SpreadCheetah.SourceGenerator.Models; + +internal readonly record struct CellStyle(string StyleNameRawString); diff --git a/SpreadCheetah.SourceGenerator/Models/PropertyAttributeData.cs b/SpreadCheetah.SourceGenerator/Models/PropertyAttributeData.cs new file mode 100644 index 00000000..a24743b1 --- /dev/null +++ b/SpreadCheetah.SourceGenerator/Models/PropertyAttributeData.cs @@ -0,0 +1,13 @@ +using System.Runtime.InteropServices; + +namespace SpreadCheetah.SourceGenerator.Models; + +[StructLayout(LayoutKind.Auto)] +internal record struct PropertyAttributeData +{ + public CellValueTruncate? CellValueTruncate { get; set; } + public CellStyle? CellStyle { get; set; } + public ColumnHeader? ColumnHeader { get; set; } + public ColumnOrder? ColumnOrder { get; set; } + public ColumnWidth? ColumnWidth { get; set; } +} \ No newline at end of file diff --git a/SpreadCheetah.SourceGenerator/Models/RowType.cs b/SpreadCheetah.SourceGenerator/Models/RowType.cs index aa7dac06..38b0ee03 100644 --- a/SpreadCheetah.SourceGenerator/Models/RowType.cs +++ b/SpreadCheetah.SourceGenerator/Models/RowType.cs @@ -6,10 +6,12 @@ internal sealed record RowType( string Name, string FullName, bool IsReferenceType, + int PropertiesWithStyleAttributes, LocationInfo? WorksheetRowAttributeLocation, EquatableArray Properties, EquatableArray UnsupportedPropertyTypeNames, EquatableArray DiagnosticInfos) { + public string CellType { get; } = PropertiesWithStyleAttributes > 0 ? "StyledCell" : "DataCell"; public string FullNameWithNullableAnnotation => IsReferenceType ? $"{FullName}?" : FullName; } \ No newline at end of file diff --git a/SpreadCheetah.SourceGenerator/Models/RowTypeProperty.cs b/SpreadCheetah.SourceGenerator/Models/RowTypeProperty.cs index 8570ebf9..e370f869 100644 --- a/SpreadCheetah.SourceGenerator/Models/RowTypeProperty.cs +++ b/SpreadCheetah.SourceGenerator/Models/RowTypeProperty.cs @@ -3,5 +3,6 @@ namespace SpreadCheetah.SourceGenerator.Models; internal sealed record RowTypeProperty( string Name, ColumnHeaderInfo? ColumnHeader, + CellStyle? CellStyle, ColumnWidth? ColumnWidth, CellValueTruncate? CellValueTruncate); \ No newline at end of file diff --git a/SpreadCheetah.SourceGenerator/WorksheetRowGenerator.cs b/SpreadCheetah.SourceGenerator/WorksheetRowGenerator.cs index 85f4729c..d0dac91f 100644 --- a/SpreadCheetah.SourceGenerator/WorksheetRowGenerator.cs +++ b/SpreadCheetah.SourceGenerator/WorksheetRowGenerator.cs @@ -86,6 +86,7 @@ private static RowType AnalyzeTypeProperties(ITypeSymbol classType, LocationInfo var explicitOrderProperties = new SortedDictionary(); var unsupportedPropertyTypeNames = new HashSet(StringComparer.Ordinal); var diagnosticInfos = new List(); + var propertiesWithStyleAttributes = 0; foreach (var property in GetClassAndBaseClassProperties(classType)) { @@ -98,26 +99,21 @@ private static RowType AnalyzeTypeProperties(ITypeSymbol classType, LocationInfo continue; } - ColumnHeader? columnHeader = null; - ColumnOrder? columnOrder = null; - ColumnWidth? columnWidth = null; - CellValueTruncate? cellValueTruncate = null; + var data = property + .GetAttributes() + .MapToPropertyAttributeData(property.Type, diagnosticInfos, token); - foreach (var attribute in property.GetAttributes()) - { - columnHeader ??= attribute.TryGetColumnHeaderAttribute(diagnosticInfos, token); - columnOrder ??= attribute.TryGetColumnOrderAttribute(token); - columnWidth ??= attribute.TryGetColumnWidthAttribute(diagnosticInfos, token); - cellValueTruncate ??= attribute.TryGetCellValueTruncateAttribute(property.Type, diagnosticInfos, token); - } + if (data.CellStyle is not null) + propertiesWithStyleAttributes++; var rowTypeProperty = new RowTypeProperty( Name: property.Name, - ColumnHeader: columnHeader?.ToColumnHeaderInfo(), - ColumnWidth: columnWidth, - CellValueTruncate: cellValueTruncate); + ColumnHeader: data.ColumnHeader?.ToColumnHeaderInfo(), + CellStyle: data.CellStyle, + ColumnWidth: data.ColumnWidth, + CellValueTruncate: data.CellValueTruncate); - if (columnOrder is not { } order) + if (data.ColumnOrder is not { } order) implicitOrderProperties.Add(rowTypeProperty); else if (!explicitOrderProperties.ContainsKey(order.Value)) explicitOrderProperties.Add(order.Value, rowTypeProperty); @@ -133,6 +129,7 @@ private static RowType AnalyzeTypeProperties(ITypeSymbol classType, LocationInfo IsReferenceType: classType.IsReferenceType, Name: classType.Name, Properties: explicitOrderProperties.Values.ToEquatableArray(), + PropertiesWithStyleAttributes: propertiesWithStyleAttributes, UnsupportedPropertyTypeNames: unsupportedPropertyTypeNames.ToEquatableArray(), WorksheetRowAttributeLocation: worksheetRowAttributeLocation); } @@ -188,6 +185,7 @@ private static void GenerateHeader(StringBuilder sb) #nullable enable using SpreadCheetah; using SpreadCheetah.SourceGeneration; + using SpreadCheetah.Styling; using System; using System.Buffers; using System.Collections.Generic; @@ -231,6 +229,8 @@ private static void GenerateCode(StringBuilder sb, ContextClass contextClass, So ++typeIndex; } + GenerateConstructTruncatedDataCell(sb); + sb.AppendLine(""" } } @@ -256,25 +256,38 @@ private static void GenerateCodeForType(StringBuilder sb, int typeIndex, RowType return; } - var doGenerateCreateWorksheetOptions = rowType.Properties.Any(static x => x.ColumnWidth is not null); - var optionalParams = doGenerateCreateWorksheetOptions - ? FormattableString.Invariant($", CreateWorksheetOptions{typeIndex}") - : ""; + var properties = rowType.Properties; + var doGenerateCreateWorksheetOptions = properties.Any(static x => x.ColumnWidth is not null); + var doGenerateCreateWorksheetRowDependencyInfo = properties.Any(static x => x.CellStyle is not null); - sb.AppendLine(FormattableString.Invariant($$""" - ??= WorksheetRowMetadataServices.CreateObjectInfo<{{rowType.FullName}}>(AddHeaderRow{{typeIndex}}Async, AddAsRowAsync, AddRangeAsRowsAsync{{optionalParams}}); + sb.Append(FormattableString.Invariant($$""" + ??= WorksheetRowMetadataServices.CreateObjectInfo<{{rowType.FullName}}>( + AddHeaderRow{{typeIndex}}Async, AddAsRowAsync, AddRangeAsRowsAsync """)); if (doGenerateCreateWorksheetOptions) - GenerateCreateWorksheetOptions(sb, typeIndex, rowType.Properties); + sb.Append(", CreateWorksheetOptions").Append(typeIndex); + else + sb.Append(", null"); + + if (doGenerateCreateWorksheetRowDependencyInfo) + sb.Append(", CreateWorksheetRowDependencyInfo").Append(typeIndex); + + sb.AppendLine(");"); + + if (doGenerateCreateWorksheetOptions) + GenerateCreateWorksheetOptions(sb, typeIndex, properties); + + var cellStyleToStyleIdIndex = doGenerateCreateWorksheetRowDependencyInfo + ? GenerateCreateWorksheetRowDependencyInfo(sb, typeIndex, properties) + : []; - GenerateAddHeaderRow(sb, typeIndex, rowType.Properties); + GenerateAddHeaderRow(sb, typeIndex, properties); GenerateAddAsRow(sb, rowType); GenerateAddRangeAsRows(sb, rowType); GenerateAddAsRowInternal(sb, rowType); GenerateAddRangeAsRowsInternal(sb, rowType); - GenerateAddEnumerableAsRows(sb, rowType); - GenerateAddCellsAsRow(sb, rowType); + GenerateAddCellsAsRow(sb, rowType, cellStyleToStyleIdIndex); } private static void ReportDiagnostics(RowType rowType, LocationInfo? locationInfo, GeneratorOptions? options, SourceProductionContext context) @@ -331,6 +344,45 @@ private static void GenerateCreateWorksheetOptions(StringBuilder sb, int typeInd """); } + private static Dictionary GenerateCreateWorksheetRowDependencyInfo( + StringBuilder sb, int typeIndex, EquatableArray properties) + { + Debug.Assert(properties.Any(static x => x.CellStyle is not null)); + + sb.AppendLine(FormattableString.Invariant($$""" + + private static WorksheetRowDependencyInfo CreateWorksheetRowDependencyInfo{{typeIndex}}(Spreadsheet spreadsheet) + { + var styleIds = new[] + { + """)); + + var cellStyleToStyleIdIndex = new Dictionary(); + + foreach (var property in properties) + { + if (property.CellStyle is not { } style) + continue; + + if (cellStyleToStyleIdIndex.ContainsKey(style)) + continue; + + cellStyleToStyleIdIndex[style] = cellStyleToStyleIdIndex.Count; + + sb.AppendLine($""" + spreadsheet.GetStyleId({style.StyleNameRawString}), + """); + } + + sb.AppendLine(""" + }; + return new WorksheetRowDependencyInfo(styleIds); + } + """); + + return cellStyleToStyleIdIndex; + } + private static void GenerateAddHeaderRow(StringBuilder sb, int typeIndex, EquatableArray properties) { Debug.Assert(properties.Count > 0); @@ -378,9 +430,9 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, {{ if (rowType.IsReferenceType) { - sb.AppendLine(""" + sb.AppendLine($""" if (obj is null) - return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); + return spreadsheet.AddRowAsync(ReadOnlyMemory<{rowType.CellType}>.Empty, token); """); } @@ -390,6 +442,46 @@ private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, {{ """); } + private static void GenerateArrayPoolRentPart(StringBuilder sb, RowType rowType) + { + var properties = rowType.Properties; + Debug.Assert(properties.Count > 0); + + sb.AppendLine($$""" + var cells = ArrayPool<{{rowType.CellType}}>.Shared.Rent({{properties.Count}}); + try + { + """); + } + + private static void GenerateGetStyleIdsPart(StringBuilder sb, RowType rowType) + { + if (rowType.PropertiesWithStyleAttributes > 0) + { + sb.AppendLine($""" + var worksheetRowDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(Default.{rowType.Name}); + var styleIds = worksheetRowDependencyInfo.StyleIds; + """); + } + else + { + sb.AppendLine(""" + var styleIds = Array.Empty(); + """); + } + } + + private static void GenerateArrayPoolReturnPart(StringBuilder sb, RowType rowType) + { + sb.AppendLine($$""" + } + finally + { + ArrayPool<{{rowType.CellType}}>.Shared.Return(cells, true); + } + """); + } + private static void GenerateAddAsRowInternal(StringBuilder sb, RowType rowType) { var properties = rowType.Properties; @@ -397,26 +489,33 @@ private static void GenerateAddAsRowInternal(StringBuilder sb, RowType rowType) sb.AppendLine($$""" - private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, {{rowType.FullName}} obj, CancellationToken token) - { - var cells = ArrayPool.Shared.Rent({{properties.Count}}); - try + private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + {{rowType.FullName}} obj, + CancellationToken token) { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); - } - finally - { - ArrayPool.Shared.Return(cells, true); + """); + + GenerateArrayPoolRentPart(sb, rowType); + GenerateGetStyleIdsPart(sb, rowType); + + sb.AppendLine(""" + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + """); + + GenerateArrayPoolReturnPart(sb, rowType); + + sb.AppendLine(""" } - } - """); + """); } private static void GenerateAddRangeAsRows(StringBuilder sb, RowType rowType) { sb.AppendLine($$""" - private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<{{rowType.FullNameWithNullableAnnotation}}> objs, CancellationToken token) + private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable<{{rowType.FullNameWithNullableAnnotation}}> objs, + CancellationToken token) { if (spreadsheet is null) throw new ArgumentNullException(nameof(spreadsheet)); @@ -432,78 +531,96 @@ private static void GenerateAddRangeAsRowsInternal(StringBuilder sb, RowType row var properties = rowType.Properties; Debug.Assert(properties.Count > 0); - sb.Append($$""" + sb.AppendLine($$""" - private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<{{rowType.FullNameWithNullableAnnotation}}> objs, CancellationToken token) - { - var cells = ArrayPool.Shared.Rent({{properties.Count}}); - try + private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, + IEnumerable<{{rowType.FullNameWithNullableAnnotation}}> objs, + CancellationToken token) { - await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false); - } - finally - { - ArrayPool.Shared.Return(cells, true); - } - } + """); - """); - } + GenerateArrayPoolRentPart(sb, rowType); + GenerateGetStyleIdsPart(sb, rowType); - private static void GenerateAddEnumerableAsRows(StringBuilder sb, RowType rowType) - { - sb.AppendLine($$""" + sb.AppendLine(""" + foreach (var obj in objs) + { + await AddCellsAsRowAsync(spreadsheet, obj, cells, styleIds, token).ConfigureAwait(false); + } + """); - private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<{{rowType.FullNameWithNullableAnnotation}}> objs, DataCell[] cells, CancellationToken token) - { - foreach (var obj in objs) - { - await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false); + GenerateArrayPoolReturnPart(sb, rowType); + + sb.AppendLine(""" } - } - """); + """); } - private static void GenerateAddCellsAsRow(StringBuilder sb, RowType rowType) + private static void GenerateAddCellsAsRow(StringBuilder sb, RowType rowType, Dictionary cellStyleToStyleIdIndex) { var properties = rowType.Properties; Debug.Assert(properties.Count > 0); sb.AppendLine($$""" - private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, {{rowType.FullNameWithNullableAnnotation}} obj, DataCell[] cells, CancellationToken token) + private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, + {{rowType.FullNameWithNullableAnnotation}} obj, + {{rowType.CellType}}[] cells, IReadOnlyList styleIds, CancellationToken token) { """); if (rowType.IsReferenceType) { - sb.AppendLine(""" + sb.AppendLine($""" if (obj is null) - return spreadsheet.AddRowAsync(ReadOnlyMemory.Empty, token); + return spreadsheet.AddRowAsync(ReadOnlyMemory<{rowType.CellType}>.Empty, token); """); } foreach (var (i, property) in properties.Index()) { - if (property.CellValueTruncate?.Value is { } truncateLength) - { - sb.AppendLine(FormattableString.Invariant($$""" - var p{{i}} = obj.{{property.Name}}; - cells[{{i}}] = p{{i}} is null || p{{i}}.Length <= {{truncateLength}} ? new DataCell(p{{i}}) : new DataCell(p{{i}}.AsMemory(0, {{truncateLength}})); - """)); - } - else - { - sb.AppendLine(FormattableString.Invariant($$""" - cells[{{i}}] = new DataCell(obj.{{property.Name}}); - """)); - } + sb.AppendLine(FormattableString.Invariant($""" + cells[{i}] = {ConstructCell(property, $"obj.{property.Name}")}; + """)); } sb.AppendLine($$""" return spreadsheet.AddRowAsync(cells.AsMemory(0, {{properties.Count}}), token); } """); + + string ConstructCell(RowTypeProperty property, string value) + { + var constructDataCell = property.CellValueTruncate is { } truncate + ? FormattableString.Invariant($"ConstructTruncatedDataCell({value}, {truncate.Value})") + : $"new DataCell({value})"; + + int? styleIdIndex = property.CellStyle is { } cellStyle + ? cellStyleToStyleIdIndex[cellStyle] + : null; + + var styledCell = rowType.PropertiesWithStyleAttributes > 0; + + return (styledCell, styleIdIndex) switch + { + (true, { } i) => FormattableString.Invariant($"new StyledCell({constructDataCell}, styleIds[{i}])"), + (true, null) => $"new StyledCell({constructDataCell}, null)", + (false, _) => constructDataCell + }; + } + } + + private static void GenerateConstructTruncatedDataCell(StringBuilder sb) + { + sb.AppendLine(""" + + private static DataCell ConstructTruncatedDataCell(string? value, int truncateLength) + { + return value is null || value.Length <= truncateLength + ? new DataCell(value) + : new DataCell(value.AsMemory(0, truncateLength)); + } + """); } } diff --git a/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet6_0.verified.txt b/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet6_0.verified.txt index b3bc1580..3287b2e6 100644 --- a/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet6_0.verified.txt +++ b/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet6_0.verified.txt @@ -123,6 +123,7 @@ namespace SpreadCheetah public System.Threading.Tasks.ValueTask DisposeAsync() { } public System.Threading.Tasks.ValueTask EmbedImageAsync(System.IO.Stream stream, System.Threading.CancellationToken token = default) { } public System.Threading.Tasks.ValueTask FinishAsync(System.Threading.CancellationToken token = default) { } + public SpreadCheetah.SourceGeneration.WorksheetRowDependencyInfo GetOrCreateWorksheetRowDependencyInfo(SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo typeInfo) { } public SpreadCheetah.Styling.StyleId GetStyleId(string name) { } public void MergeCells(string cellRange) { } public System.Threading.Tasks.ValueTask StartWorksheetAsync(string name, SpreadCheetah.Worksheets.WorksheetOptions? options = null, System.Threading.CancellationToken token = default) { } @@ -184,6 +185,11 @@ namespace SpreadCheetah.Images } namespace SpreadCheetah.SourceGeneration { + [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)] + public sealed class CellStyleAttribute : System.Attribute + { + public CellStyleAttribute(string styleName) { } + } [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)] public sealed class CellValueTruncateAttribute : System.Attribute { @@ -229,6 +235,11 @@ namespace SpreadCheetah.SourceGeneration { protected WorksheetRowContext() { } } + public sealed class WorksheetRowDependencyInfo : System.IEquatable + { + public WorksheetRowDependencyInfo(System.Collections.Generic.IReadOnlyList StyleIds) { } + public System.Collections.Generic.IReadOnlyList StyleIds { get; init; } + } [System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)] public sealed class WorksheetRowGenerationOptionsAttribute : System.Attribute { @@ -237,10 +248,11 @@ namespace SpreadCheetah.SourceGeneration } public static class WorksheetRowMetadataServices { - public static SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo CreateObjectInfo(System.Func headerHandler, System.Func rowHandler, System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> rowRangeHandler, System.Func? worksheetOptionsFactory = null) { } + public static SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo CreateObjectInfo(System.Func headerHandler, System.Func rowHandler, System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> rowRangeHandler, System.Func? worksheetOptionsFactory = null, System.Func? createWorksheetRowDependencyInfo = null) { } } public abstract class WorksheetRowTypeInfo { + public System.Func? CreateWorksheetRowDependencyInfo { get; } public System.Func HeaderHandler { get; } public System.Func RowHandler { get; } public System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> RowRangeHandler { get; } diff --git a/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet7_0.verified.txt b/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet7_0.verified.txt index 44fbb6c6..cc45cc5d 100644 --- a/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet7_0.verified.txt +++ b/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet7_0.verified.txt @@ -123,6 +123,7 @@ namespace SpreadCheetah public System.Threading.Tasks.ValueTask DisposeAsync() { } public System.Threading.Tasks.ValueTask EmbedImageAsync(System.IO.Stream stream, System.Threading.CancellationToken token = default) { } public System.Threading.Tasks.ValueTask FinishAsync(System.Threading.CancellationToken token = default) { } + public SpreadCheetah.SourceGeneration.WorksheetRowDependencyInfo GetOrCreateWorksheetRowDependencyInfo(SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo typeInfo) { } public SpreadCheetah.Styling.StyleId GetStyleId(string name) { } public void MergeCells(string cellRange) { } public System.Threading.Tasks.ValueTask StartWorksheetAsync(string name, SpreadCheetah.Worksheets.WorksheetOptions? options = null, System.Threading.CancellationToken token = default) { } @@ -184,6 +185,11 @@ namespace SpreadCheetah.Images } namespace SpreadCheetah.SourceGeneration { + [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)] + public sealed class CellStyleAttribute : System.Attribute + { + public CellStyleAttribute(string styleName) { } + } [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)] public sealed class CellValueTruncateAttribute : System.Attribute { @@ -229,6 +235,11 @@ namespace SpreadCheetah.SourceGeneration { protected WorksheetRowContext() { } } + public sealed class WorksheetRowDependencyInfo : System.IEquatable + { + public WorksheetRowDependencyInfo(System.Collections.Generic.IReadOnlyList StyleIds) { } + public System.Collections.Generic.IReadOnlyList StyleIds { get; init; } + } [System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)] public sealed class WorksheetRowGenerationOptionsAttribute : System.Attribute { @@ -237,10 +248,11 @@ namespace SpreadCheetah.SourceGeneration } public static class WorksheetRowMetadataServices { - public static SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo CreateObjectInfo(System.Func headerHandler, System.Func rowHandler, System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> rowRangeHandler, System.Func? worksheetOptionsFactory = null) { } + public static SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo CreateObjectInfo(System.Func headerHandler, System.Func rowHandler, System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> rowRangeHandler, System.Func? worksheetOptionsFactory = null, System.Func? createWorksheetRowDependencyInfo = null) { } } public abstract class WorksheetRowTypeInfo { + public System.Func? CreateWorksheetRowDependencyInfo { get; } public System.Func HeaderHandler { get; } public System.Func RowHandler { get; } public System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> RowRangeHandler { get; } diff --git a/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet8_0.verified.txt b/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet8_0.verified.txt index 2e71d9a9..142ffde8 100644 --- a/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet8_0.verified.txt +++ b/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.DotNet8_0.verified.txt @@ -123,6 +123,7 @@ namespace SpreadCheetah public System.Threading.Tasks.ValueTask DisposeAsync() { } public System.Threading.Tasks.ValueTask EmbedImageAsync(System.IO.Stream stream, System.Threading.CancellationToken token = default) { } public System.Threading.Tasks.ValueTask FinishAsync(System.Threading.CancellationToken token = default) { } + public SpreadCheetah.SourceGeneration.WorksheetRowDependencyInfo GetOrCreateWorksheetRowDependencyInfo(SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo typeInfo) { } public SpreadCheetah.Styling.StyleId GetStyleId(string name) { } public void MergeCells(string cellRange) { } public System.Threading.Tasks.ValueTask StartWorksheetAsync(string name, SpreadCheetah.Worksheets.WorksheetOptions? options = null, System.Threading.CancellationToken token = default) { } @@ -184,6 +185,11 @@ namespace SpreadCheetah.Images } namespace SpreadCheetah.SourceGeneration { + [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)] + public sealed class CellStyleAttribute : System.Attribute + { + public CellStyleAttribute(string styleName) { } + } [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)] public sealed class CellValueTruncateAttribute : System.Attribute { @@ -229,6 +235,11 @@ namespace SpreadCheetah.SourceGeneration { protected WorksheetRowContext() { } } + public sealed class WorksheetRowDependencyInfo : System.IEquatable + { + public WorksheetRowDependencyInfo(System.Collections.Generic.IReadOnlyList StyleIds) { } + public System.Collections.Generic.IReadOnlyList StyleIds { get; init; } + } [System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)] public sealed class WorksheetRowGenerationOptionsAttribute : System.Attribute { @@ -237,10 +248,11 @@ namespace SpreadCheetah.SourceGeneration } public static class WorksheetRowMetadataServices { - public static SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo CreateObjectInfo(System.Func headerHandler, System.Func rowHandler, System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> rowRangeHandler, System.Func? worksheetOptionsFactory = null) { } + public static SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo CreateObjectInfo(System.Func headerHandler, System.Func rowHandler, System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> rowRangeHandler, System.Func? worksheetOptionsFactory = null, System.Func? createWorksheetRowDependencyInfo = null) { } } public abstract class WorksheetRowTypeInfo { + public System.Func? CreateWorksheetRowDependencyInfo { get; } public System.Func HeaderHandler { get; } public System.Func RowHandler { get; } public System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> RowRangeHandler { get; } diff --git a/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.Net4_7.verified.txt b/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.Net4_7.verified.txt index a6c5fe3f..2f76062f 100644 --- a/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.Net4_7.verified.txt +++ b/SpreadCheetah.Test/Tests/PublicApiTests.PublicApi_Generate.Net4_7.verified.txt @@ -122,6 +122,7 @@ namespace SpreadCheetah public System.Threading.Tasks.ValueTask DisposeAsync() { } public System.Threading.Tasks.ValueTask EmbedImageAsync(System.IO.Stream stream, System.Threading.CancellationToken token = default) { } public System.Threading.Tasks.ValueTask FinishAsync(System.Threading.CancellationToken token = default) { } + public SpreadCheetah.SourceGeneration.WorksheetRowDependencyInfo GetOrCreateWorksheetRowDependencyInfo(SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo typeInfo) { } public SpreadCheetah.Styling.StyleId GetStyleId(string name) { } public void MergeCells(string cellRange) { } public System.Threading.Tasks.ValueTask StartWorksheetAsync(string name, SpreadCheetah.Worksheets.WorksheetOptions? options = null, System.Threading.CancellationToken token = default) { } @@ -183,6 +184,11 @@ namespace SpreadCheetah.Images } namespace SpreadCheetah.SourceGeneration { + [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)] + public sealed class CellStyleAttribute : System.Attribute + { + public CellStyleAttribute(string styleName) { } + } [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple=false)] public sealed class CellValueTruncateAttribute : System.Attribute { @@ -228,6 +234,11 @@ namespace SpreadCheetah.SourceGeneration { protected WorksheetRowContext() { } } + public sealed class WorksheetRowDependencyInfo : System.IEquatable + { + public WorksheetRowDependencyInfo(System.Collections.Generic.IReadOnlyList StyleIds) { } + public System.Collections.Generic.IReadOnlyList StyleIds { get; init; } + } [System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)] public sealed class WorksheetRowGenerationOptionsAttribute : System.Attribute { @@ -236,10 +247,11 @@ namespace SpreadCheetah.SourceGeneration } public static class WorksheetRowMetadataServices { - public static SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo CreateObjectInfo(System.Func headerHandler, System.Func rowHandler, System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> rowRangeHandler, System.Func? worksheetOptionsFactory = null) { } + public static SpreadCheetah.SourceGeneration.WorksheetRowTypeInfo CreateObjectInfo(System.Func headerHandler, System.Func rowHandler, System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> rowRangeHandler, System.Func? worksheetOptionsFactory = null, System.Func? createWorksheetRowDependencyInfo = null) { } } public abstract class WorksheetRowTypeInfo { + public System.Func? CreateWorksheetRowDependencyInfo { get; } public System.Func HeaderHandler { get; } public System.Func RowHandler { get; } public System.Func, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask> RowRangeHandler { get; } diff --git a/SpreadCheetah.Test/Tests/SpreadsheetStyledRowTests.cs b/SpreadCheetah.Test/Tests/SpreadsheetStyledRowTests.cs index a31d4a29..243a013e 100644 --- a/SpreadCheetah.Test/Tests/SpreadsheetStyledRowTests.cs +++ b/SpreadCheetah.Test/Tests/SpreadsheetStyledRowTests.cs @@ -13,6 +13,7 @@ using Fill = SpreadCheetah.Styling.Fill; using Font = SpreadCheetah.Styling.Font; using OpenXmlCell = DocumentFormat.OpenXml.Spreadsheet.Cell; +using SpreadsheetAssert = SpreadCheetah.TestHelpers.Assertions.SpreadsheetAssert; namespace SpreadCheetah.Test.Tests; @@ -58,26 +59,22 @@ public async Task Spreadsheet_AddRow_BoldCellWithStringValue(bool bold, CellType // Arrange const string cellValue = "Bold test"; using var stream = new MemoryStream(); - await using (var spreadsheet = await Spreadsheet.CreateNewAsync(stream)) - { - await spreadsheet.StartWorksheetAsync("Sheet"); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); - var style = new Style(); - style.Font.Bold = bold; - var styleId = spreadsheet.AddStyle(style); - var styledCell = CellFactory.Create(type, cellValue, styleId); + var style = new Style(); + style.Font.Bold = bold; + var styleId = spreadsheet.AddStyle(style); + var styledCell = CellFactory.Create(type, cellValue, styleId); - // Act - await spreadsheet.AddRowAsync(styledCell, rowType); - await spreadsheet.FinishAsync(); - } + // Act + await spreadsheet.AddRowAsync(styledCell, rowType); + await spreadsheet.FinishAsync(); // Assert - SpreadsheetAssert.Valid(stream); - using var workbook = new XLWorkbook(stream); - var worksheet = workbook.Worksheets.Single(); - var actualCell = worksheet.Cell(1, 1); - Assert.Equal(cellValue, actualCell.Value); + using var sheet = SpreadsheetAssert.SingleSheet(stream); + var actualCell = sheet["A1"]; + Assert.Equal(cellValue, actualCell.StringValue); Assert.Equal(bold, actualCell.Style.Font.Bold); } @@ -89,30 +86,26 @@ public async Task Spreadsheet_AddRow_SameBoldStyleCells(bool bold, CellType type const string firstCellValue = "First"; const string secondCellValue = "Second"; using var stream = new MemoryStream(); - await using (var spreadsheet = await Spreadsheet.CreateNewAsync(stream)) - { - await spreadsheet.StartWorksheetAsync("Sheet"); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); - var style = new Style(); - style.Font.Bold = bold; - var styleId = spreadsheet.AddStyle(style); + var style = new Style(); + style.Font.Bold = bold; + var styleId = spreadsheet.AddStyle(style); - var firstCell = CellFactory.Create(type, firstCellValue, styleId); - var secondCell = CellFactory.Create(type, secondCellValue, styleId); + var firstCell = CellFactory.Create(type, firstCellValue, styleId); + var secondCell = CellFactory.Create(type, secondCellValue, styleId); - // Act - await spreadsheet.AddRowAsync(new[] { firstCell, secondCell }, rowType); - await spreadsheet.FinishAsync(); - } + // Act + await spreadsheet.AddRowAsync([firstCell, secondCell], rowType); + await spreadsheet.FinishAsync(); // Assert - SpreadsheetAssert.Valid(stream); - using var workbook = new XLWorkbook(stream); - var worksheet = workbook.Worksheets.Single(); - var actualFirstCell = worksheet.Cell(1, 1); - var actualSecondCell = worksheet.Cell(1, 2); - Assert.Equal(firstCellValue, actualFirstCell.Value); - Assert.Equal(secondCellValue, actualSecondCell.Value); + using var sheet = SpreadsheetAssert.SingleSheet(stream); + var actualFirstCell = sheet["A1"]; + var actualSecondCell = sheet["B1"]; + Assert.Equal(firstCellValue, actualFirstCell.StringValue); + Assert.Equal(secondCellValue, actualSecondCell.StringValue); Assert.Equal(bold, actualFirstCell.Style.Font.Bold); Assert.Equal(bold, actualSecondCell.Style.Font.Bold); } @@ -125,30 +118,26 @@ public async Task Spreadsheet_AddRow_MixedBoldStyleCells(bool firstCellBold, Cel const string firstCellValue = "First"; const string secondCellValue = "Second"; using var stream = new MemoryStream(); - await using (var spreadsheet = await Spreadsheet.CreateNewAsync(stream)) - { - await spreadsheet.StartWorksheetAsync("Sheet"); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); - var style = new Style(); - style.Font.Bold = true; - var styleId = spreadsheet.AddStyle(style); + var style = new Style(); + style.Font.Bold = true; + var styleId = spreadsheet.AddStyle(style); - var firstCell = CellFactory.Create(type, firstCellValue, firstCellBold ? styleId : null); - var secondCell = CellFactory.Create(type, secondCellValue, firstCellBold ? null : styleId); + var firstCell = CellFactory.Create(type, firstCellValue, firstCellBold ? styleId : null); + var secondCell = CellFactory.Create(type, secondCellValue, firstCellBold ? null : styleId); - // Act - await spreadsheet.AddRowAsync(new[] { firstCell, secondCell }, rowType); - await spreadsheet.FinishAsync(); - } + // Act + await spreadsheet.AddRowAsync([firstCell, secondCell], rowType); + await spreadsheet.FinishAsync(); // Assert - SpreadsheetAssert.Valid(stream); - using var workbook = new XLWorkbook(stream); - var worksheet = workbook.Worksheets.Single(); - var actualFirstCell = worksheet.Cell(1, 1); - var actualSecondCell = worksheet.Cell(1, 2); - Assert.Equal(firstCellValue, actualFirstCell.Value); - Assert.Equal(secondCellValue, actualSecondCell.Value); + using var sheet = SpreadsheetAssert.SingleSheet(stream); + var actualFirstCell = sheet["A1"]; + var actualSecondCell = sheet["B1"]; + Assert.Equal(firstCellValue, actualFirstCell.StringValue); + Assert.Equal(secondCellValue, actualSecondCell.StringValue); Assert.Equal(firstCellBold, actualFirstCell.Style.Font.Bold); Assert.Equal(!firstCellBold, actualSecondCell.Style.Font.Bold); } @@ -160,26 +149,22 @@ public async Task Spreadsheet_AddRow_ItalicCellWithStringValue(bool italic, Cell // Arrange const string cellValue = "Italic test"; using var stream = new MemoryStream(); - await using (var spreadsheet = await Spreadsheet.CreateNewAsync(stream)) - { - await spreadsheet.StartWorksheetAsync("Sheet"); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); - var style = new Style(); - style.Font.Italic = italic; - var styleId = spreadsheet.AddStyle(style); - var styledCell = CellFactory.Create(type, cellValue, styleId); + var style = new Style(); + style.Font.Italic = italic; + var styleId = spreadsheet.AddStyle(style); + var styledCell = CellFactory.Create(type, cellValue, styleId); - // Act - await spreadsheet.AddRowAsync(styledCell, rowType); - await spreadsheet.FinishAsync(); - } + // Act + await spreadsheet.AddRowAsync(styledCell, rowType); + await spreadsheet.FinishAsync(); // Assert - SpreadsheetAssert.Valid(stream); - using var workbook = new XLWorkbook(stream); - var worksheet = workbook.Worksheets.Single(); - var actualCell = worksheet.Cell(1, 1); - Assert.Equal(cellValue, actualCell.Value); + using var sheet = SpreadsheetAssert.SingleSheet(stream); + var actualCell = sheet["A1"]; + Assert.Equal(cellValue, actualCell.StringValue); Assert.Equal(italic, actualCell.Style.Font.Italic); } @@ -921,37 +906,33 @@ public async Task Spreadsheet_AddRow_MixedCellTypeRows(bool firstRowStyled, Cell const string firstCellValue = "First"; const string secondCellValue = "Second"; using var stream = new MemoryStream(); - await using (var spreadsheet = await Spreadsheet.CreateNewAsync(stream)) - { - await spreadsheet.StartWorksheetAsync("Sheet"); - - var style = new Style(); - style.Font.Bold = true; - var styleId = spreadsheet.AddStyle(style); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); - // Act - if (firstRowStyled) - { - await spreadsheet.AddRowAsync(CellFactory.Create(type, firstCellValue, styleId), rowType); - await spreadsheet.AddRowAsync(new DataCell(secondCellValue), rowType); - } - else - { - await spreadsheet.AddRowAsync(new DataCell(firstCellValue), rowType); - await spreadsheet.AddRowAsync(CellFactory.Create(type, secondCellValue, styleId), rowType); - } + var style = new Style(); + style.Font.Bold = true; + var styleId = spreadsheet.AddStyle(style); - await spreadsheet.FinishAsync(); + // Act + if (firstRowStyled) + { + await spreadsheet.AddRowAsync(CellFactory.Create(type, firstCellValue, styleId), rowType); + await spreadsheet.AddRowAsync(new DataCell(secondCellValue), rowType); + } + else + { + await spreadsheet.AddRowAsync(new DataCell(firstCellValue), rowType); + await spreadsheet.AddRowAsync(CellFactory.Create(type, secondCellValue, styleId), rowType); } + await spreadsheet.FinishAsync(); + // Assert - SpreadsheetAssert.Valid(stream); - using var workbook = new XLWorkbook(stream); - var worksheet = workbook.Worksheets.Single(); - var actualFirstCell = worksheet.Cell(1, 1); - var actualSecondCell = worksheet.Cell(2, 1); - Assert.Equal(firstCellValue, actualFirstCell.Value); - Assert.Equal(secondCellValue, actualSecondCell.Value); + using var sheet = SpreadsheetAssert.SingleSheet(stream); + var actualFirstCell = sheet["A1"]; + var actualSecondCell = sheet["A2"]; + Assert.Equal(firstCellValue, actualFirstCell.StringValue); + Assert.Equal(secondCellValue, actualSecondCell.StringValue); Assert.Equal(firstRowStyled, actualFirstCell.Style.Font.Bold); Assert.Equal(!firstRowStyled, actualSecondCell.Style.Font.Bold); } @@ -963,29 +944,25 @@ public async Task Spreadsheet_AddRow_StyleChangeAfterAddStyleNotApplied(bool cha { // Arrange using var stream = new MemoryStream(); - await using (var spreadsheet = await Spreadsheet.CreateNewAsync(stream)) - { - await spreadsheet.StartWorksheetAsync("Sheet"); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream); + await spreadsheet.StartWorksheetAsync("Sheet"); - var style = new Style(); - style.Font.Bold = true; + var style = new Style(); + style.Font.Bold = true; - if (changedBefore) style.Font.Italic = true; - var styleId = spreadsheet.AddStyle(style); - if (!changedBefore) style.Font.Italic = true; + if (changedBefore) style.Font.Italic = true; + var styleId = spreadsheet.AddStyle(style); + if (!changedBefore) style.Font.Italic = true; - var styledCell = new StyledCell("value", styleId); + var styledCell = new StyledCell("value", styleId); - // Act - await spreadsheet.AddRowAsync(styledCell); - await spreadsheet.FinishAsync(); - } + // Act + await spreadsheet.AddRowAsync(styledCell); + await spreadsheet.FinishAsync(); // Assert - SpreadsheetAssert.Valid(stream); - using var workbook = new XLWorkbook(stream); - var worksheet = workbook.Worksheets.Single(); - var actualCell = worksheet.Cell(1, 1); + using var sheet = SpreadsheetAssert.SingleSheet(stream); + var actualCell = sheet["A1"]; Assert.True(actualCell.Style.Font.Bold); Assert.Equal(changedBefore, actualCell.Style.Font.Italic); } @@ -1019,7 +996,7 @@ public async Task Spreadsheet_AddRow_MultipleStyles() using var workbook = new XLWorkbook(stream); var worksheet = workbook.Worksheets.Single(); var actualStyles = worksheet.Cells().Select(x => x.Style).ToList(); - Assert.All(styles.Zip(actualStyles), x => SpreadsheetAssert.EquivalentStyle(x.First, x.Second)); + Assert.All(styles.Zip(actualStyles), x => SpreadCheetah.Test.Helpers.SpreadsheetAssert.EquivalentStyle(x.First, x.Second)); } [Theory] diff --git a/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertCell.cs b/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertCell.cs index 769faf2f..1c9f07fa 100644 --- a/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertCell.cs +++ b/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertCell.cs @@ -10,5 +10,7 @@ internal sealed class ClosedXmlAssertCell(IXLCell cell) : ISpreadsheetAssertCell public string? StringValue => cell.Value.IsBlank ? null : cell.GetText(); + public DateTime? DateTimeValue => cell.GetValue() is { } value ? DateTime.FromOADate(value) : null; + public ISpreadsheetAssertStyle Style => new ClosedXmlAssertStyle(cell.Style); } diff --git a/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyle.cs b/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyle.cs index e329a36e..0bab9028 100644 --- a/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyle.cs +++ b/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyle.cs @@ -5,4 +5,6 @@ namespace SpreadCheetah.TestHelpers.Assertions; internal sealed class ClosedXmlAssertStyle(IXLStyle style) : ISpreadsheetAssertStyle { public ISpreadsheetAssertStyleFont Font => new ClosedXmlAssertStyleFont(style.Font); + + public ISpreadsheetAssertStyleNumberFormat NumberFormat => new ClosedXmlAssertStyleNumberFormat(style.NumberFormat); } diff --git a/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyleFont.cs b/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyleFont.cs index 3d129f3b..2bfddfcf 100644 --- a/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyleFont.cs +++ b/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyleFont.cs @@ -5,4 +5,5 @@ namespace SpreadCheetah.TestHelpers.Assertions; internal sealed class ClosedXmlAssertStyleFont(IXLFont font) : ISpreadsheetAssertStyleFont { public bool Bold => font.Bold; + public bool Italic => font.Italic; } diff --git a/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyleNumberFormat.cs b/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyleNumberFormat.cs new file mode 100644 index 00000000..ac267830 --- /dev/null +++ b/SpreadCheetah.TestHelpers/Assertions/ClosedXmlAssertStyleNumberFormat.cs @@ -0,0 +1,9 @@ +using ClosedXML.Excel; + +namespace SpreadCheetah.TestHelpers.Assertions; + +internal sealed class ClosedXmlAssertStyleNumberFormat(IXLNumberFormat numberFormat) + : ISpreadsheetAssertStyleNumberFormat +{ + public string? Format => numberFormat.Format is { Length: > 0 } format ? format : null; +} diff --git a/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertCell.cs b/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertCell.cs index 3d474f09..310caacd 100644 --- a/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertCell.cs +++ b/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertCell.cs @@ -5,5 +5,6 @@ public interface ISpreadsheetAssertCell int? IntValue { get; } decimal? DecimalValue { get; } string? StringValue { get; } + DateTime? DateTimeValue { get; } ISpreadsheetAssertStyle Style { get; } } diff --git a/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyle.cs b/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyle.cs index 588b2bc2..5a207f76 100644 --- a/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyle.cs +++ b/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyle.cs @@ -3,4 +3,5 @@ namespace SpreadCheetah.TestHelpers.Assertions; public interface ISpreadsheetAssertStyle { ISpreadsheetAssertStyleFont Font { get; } + ISpreadsheetAssertStyleNumberFormat NumberFormat { get; } } diff --git a/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyleFont.cs b/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyleFont.cs index 11d8894e..5dc6e645 100644 --- a/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyleFont.cs +++ b/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyleFont.cs @@ -3,4 +3,5 @@ namespace SpreadCheetah.TestHelpers.Assertions; public interface ISpreadsheetAssertStyleFont { bool Bold { get; } + bool Italic { get; } } diff --git a/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyleNumberFormat.cs b/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyleNumberFormat.cs new file mode 100644 index 00000000..bb931a6c --- /dev/null +++ b/SpreadCheetah.TestHelpers/Assertions/ISpreadsheetAssertStyleNumberFormat.cs @@ -0,0 +1,6 @@ +namespace SpreadCheetah.TestHelpers.Assertions; + +public interface ISpreadsheetAssertStyleNumberFormat +{ + string? Format { get; } +} diff --git a/SpreadCheetah/Helpers/ThrowHelper.cs b/SpreadCheetah/Helpers/ThrowHelper.cs index a838226f..0ab9e903 100644 --- a/SpreadCheetah/Helpers/ThrowHelper.cs +++ b/SpreadCheetah/Helpers/ThrowHelper.cs @@ -83,7 +83,7 @@ internal static class ThrowHelper public static void StyleNameCanNotEqualNormal(string? paramName) => throw new ArgumentException("The name can not be equal to 'Normal', since that is the name of the default built-in style.", paramName); [DoesNotReturn] - public static void StyleNameNotFound(string name) => throw new SpreadCheetahException($"Style with name '{name}' was not found. Make sure the style is first added to the spreadsheet by calling {nameof(Spreadsheet.AddStyle)} with a name argument."); + public static void StyleNameNotFound(string name) => throw new SpreadCheetahException($"Style with name '{name}' was not found. Make sure the style is first added to the spreadsheet by calling {nameof(Spreadsheet)}.{nameof(Spreadsheet.AddStyle)} with a name argument."); [DoesNotReturn] public static void StyleNameStartsOrEndsWithWhiteSpace(string? paramName) => throw new ArgumentException("The name can not start or end with white-space.", paramName); diff --git a/SpreadCheetah/SourceGeneration/CellStyleAttribute.cs b/SpreadCheetah/SourceGeneration/CellStyleAttribute.cs new file mode 100644 index 00000000..11eac247 --- /dev/null +++ b/SpreadCheetah/SourceGeneration/CellStyleAttribute.cs @@ -0,0 +1,11 @@ +using SpreadCheetah.Styling; + +namespace SpreadCheetah.SourceGeneration; + +/// +/// Instructs the SpreadCheetah source generator to apply a named style to the cells created for a property. +/// The named style must first be added with +/// before it can be used by the source generator. +/// +[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] +public sealed class CellStyleAttribute(string styleName) : Attribute; diff --git a/SpreadCheetah/SourceGeneration/CellValueTruncateAttribute.cs b/SpreadCheetah/SourceGeneration/CellValueTruncateAttribute.cs index 0c4161a1..df3b3293 100644 --- a/SpreadCheetah/SourceGeneration/CellValueTruncateAttribute.cs +++ b/SpreadCheetah/SourceGeneration/CellValueTruncateAttribute.cs @@ -1,7 +1,7 @@ namespace SpreadCheetah.SourceGeneration; /// -/// Instructs the SpreadCheetah source generator to truncate the cell value for a property to the specified length. +/// Instructs the SpreadCheetah source generator to truncate the cell values for a property to the specified length. /// The attribute is currently only supported on properties. /// The specified length must be greater than 0. /// diff --git a/SpreadCheetah/SourceGeneration/Internal/WorksheetRowTypeInfoInternal.cs b/SpreadCheetah/SourceGeneration/Internal/WorksheetRowTypeInfoInternal.cs index ab3eb80e..50c4e76f 100644 --- a/SpreadCheetah/SourceGeneration/Internal/WorksheetRowTypeInfoInternal.cs +++ b/SpreadCheetah/SourceGeneration/Internal/WorksheetRowTypeInfoInternal.cs @@ -7,5 +7,6 @@ internal sealed class WorksheetRowTypeInfoInternal( Func headerHandler, Func rowHandler, Func, CancellationToken, ValueTask> rowRangeHandler, - Func? worksheetOptionsFactory) - : WorksheetRowTypeInfo(headerHandler, rowHandler, rowRangeHandler, worksheetOptionsFactory); \ No newline at end of file + Func? worksheetOptionsFactory, + Func? createWorksheetRowDependencyInfo) + : WorksheetRowTypeInfo(headerHandler, rowHandler, rowRangeHandler, worksheetOptionsFactory, createWorksheetRowDependencyInfo); \ No newline at end of file diff --git a/SpreadCheetah/SourceGeneration/WorksheetRowDependencyInfo.cs b/SpreadCheetah/SourceGeneration/WorksheetRowDependencyInfo.cs new file mode 100644 index 00000000..792dacf4 --- /dev/null +++ b/SpreadCheetah/SourceGeneration/WorksheetRowDependencyInfo.cs @@ -0,0 +1,10 @@ +using SpreadCheetah.Styling; + +namespace SpreadCheetah.SourceGeneration; + +/// +/// Used by code generated by the source generator and is not intended to be used directly. +/// This class is used to cache dependencies to avoid redundant lookups. +/// The class is immutable and the cache will return the same instance once it has been created. +/// +public sealed record WorksheetRowDependencyInfo(IReadOnlyList StyleIds); \ No newline at end of file diff --git a/SpreadCheetah/SourceGeneration/WorksheetRowMetadataServices.cs b/SpreadCheetah/SourceGeneration/WorksheetRowMetadataServices.cs index c62e067e..b212177f 100644 --- a/SpreadCheetah/SourceGeneration/WorksheetRowMetadataServices.cs +++ b/SpreadCheetah/SourceGeneration/WorksheetRowMetadataServices.cs @@ -5,17 +5,20 @@ namespace SpreadCheetah.SourceGeneration; /// -/// Provides helpers used by code generated by the SpreadCheetah source generator. +/// Used by code generated by the source generator and is not intended to be used directly. +/// Provides helpers used by code generated by the source generator. /// public static class WorksheetRowMetadataServices { /// + /// Used by code generated by the source generator and is not intended to be used directly. /// Creates metadata for a row type. /// public static WorksheetRowTypeInfo CreateObjectInfo( Func headerHandler, Func rowHandler, Func, CancellationToken, ValueTask> rowRangeHandler, - Func? worksheetOptionsFactory = null) - => new WorksheetRowTypeInfoInternal(headerHandler, rowHandler, rowRangeHandler, worksheetOptionsFactory); + Func? worksheetOptionsFactory = null, + Func? createWorksheetRowDependencyInfo = null) + => new WorksheetRowTypeInfoInternal(headerHandler, rowHandler, rowRangeHandler, worksheetOptionsFactory, createWorksheetRowDependencyInfo); } diff --git a/SpreadCheetah/SourceGeneration/WorksheetRowTypeInfo.cs b/SpreadCheetah/SourceGeneration/WorksheetRowTypeInfo.cs index 330e6892..3d1b2672 100644 --- a/SpreadCheetah/SourceGeneration/WorksheetRowTypeInfo.cs +++ b/SpreadCheetah/SourceGeneration/WorksheetRowTypeInfo.cs @@ -23,6 +23,11 @@ public abstract class WorksheetRowTypeInfo /// public WorksheetOptions CreateWorksheetOptions() => _worksheetOptionsFactory?.Invoke() ?? new(); + /// + /// Method for creating a class of dependencies, that will be cached to avoid redundant lookups. + /// + public Func? CreateWorksheetRowDependencyInfo { get; } + /// /// Method for adding a header to a worksheet from a type. /// @@ -42,11 +47,13 @@ private protected WorksheetRowTypeInfo( Func headerHandler, Func rowHandler, Func, CancellationToken, ValueTask> rowRangeHandler, - Func? worksheetOptionsFactory) + Func? worksheetOptionsFactory, + Func? createWorksheetRowDependencyInfo) { + _worksheetOptionsFactory = worksheetOptionsFactory; + CreateWorksheetRowDependencyInfo = createWorksheetRowDependencyInfo; HeaderHandler = headerHandler; RowHandler = rowHandler; RowRangeHandler = rowRangeHandler; - _worksheetOptionsFactory = worksheetOptionsFactory; } } diff --git a/SpreadCheetah/Spreadsheet.cs b/SpreadCheetah/Spreadsheet.cs index 6947ae06..9dc6e3db 100644 --- a/SpreadCheetah/Spreadsheet.cs +++ b/SpreadCheetah/Spreadsheet.cs @@ -29,6 +29,7 @@ public sealed class Spreadsheet : IDisposable, IAsyncDisposable private readonly CompressionLevel _compressionLevel; private readonly SpreadsheetBuffer _buffer; private readonly bool _writeCellReferenceAttributes; + private Dictionary? _worksheetRowDependencyInfo; private FileCounter? _fileCounter; private StyleManager? _styleManager; private Worksheet? _worksheet; @@ -377,6 +378,23 @@ public ValueTask AddRangeAsRowsAsync(IEnumerable objs, WorksheetRowTypeInf return typeInfo.RowRangeHandler(this, objs, token); } + /// + /// Used by code generated by the source generator and is not intended to be used directly. + /// This method is used to cache dependencies to avoid redundant lookups. + /// The return type is immutable and the cache will return the same instance once it has been created. + /// + public WorksheetRowDependencyInfo GetOrCreateWorksheetRowDependencyInfo(WorksheetRowTypeInfo typeInfo) + { + var dictionary = _worksheetRowDependencyInfo ??= []; + if (dictionary.TryGetValue(typeof(T), out var result)) + return result; + + ArgumentNullException.ThrowIfNull(typeInfo); + result = typeInfo.CreateWorksheetRowDependencyInfo?.Invoke(this) ?? new([]); + dictionary[typeof(T)] = result; + return result; + } + /// /// Adds a reusable style to the spreadsheet and returns a style ID. ///