Skip to content

Commit

Permalink
Merge pull request #42 from sveinungf/dev/columnheader-resx
Browse files Browse the repository at this point in the history
Source generator - Property reference ColumnHeader
  • Loading branch information
sveinungf authored Mar 17, 2024
2 parents abc28c6 + af40c3d commit 66f5a6d
Show file tree
Hide file tree
Showing 37 changed files with 1,213 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ dotnet_diagnostic.MA0076.severity = none
# RCS1163: Unused parameter
dotnet_diagnostic.RCS1163.severity = warning

[{ColumnHeaderAttribute.cs,ColumnOrderAttribute.cs,WorksheetRowAttribute.cs}]
# RCS1163: Unused parameter
dotnet_diagnostic.RCS1163.severity = none

###############################
# SonarAnalyzer Options #
###############################
Expand Down
3 changes: 3 additions & 0 deletions SpreadCheetah.SourceGenerator.SnapshotTest/Assembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Resources;

[assembly: NeutralResourcesLanguage("en-US")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using SpreadCheetah.SourceGeneration;

namespace SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader;

public class ClassWithPropertyReferenceColumnHeaders
{
[ColumnHeader(typeof(ColumnHeaderResources), nameof(ColumnHeaderResources.Header_FirstName))]
public string? FirstName { get; set; }

[ColumnHeader(propertyName: nameof(ColumnHeaderResources.Header_LastName), type: typeof(ColumnHeaderResources))]
public string? LastName { get; set; }

[ColumnHeader(typeof(ColumnHeaders), nameof(ColumnHeaders.HeaderNationality))]
public string? Nationality { get; set; }

[ColumnHeader(typeof(ColumnHeaders), nameof(ColumnHeaders.HeaderAddressLine1))]
public string? AddressLine1 { get; set; }

[ColumnHeader(typeof(ColumnHeaders), nameof(ColumnHeaders.HeaderAddressLine2))]
public string? AddressLine2 { get; set; }

[ColumnHeader(typeof(ColumnHeaders), nameof(ColumnHeaders.HeaderAge))]
public int Age { get; set; }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Header_FirstName" xml:space="preserve">
<value>First name</value>
</data>
<data name="Header_LastName" xml:space="preserve">
<value>Last name</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SpreadCheetah.SourceGenerator.SnapshotTest.Models.ColumnHeader;

public static class ColumnHeaders
{
public static string HeaderNationality { get; } = "The nationality";
public static string HeaderAddressLine1 => "Address line 1";
public static string? HeaderAddressLine2 => null;
public static string? HeaderAge => $"Age (in {DateTime.UtcNow.Year})";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//HintName: MyNamespace.MyGenRowContext.g.cs
// <auto-generated />
#nullable enable
using SpreadCheetah;
using SpreadCheetah.SourceGeneration;
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<MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders>? _ClassWithInvalidPropertyReferenceColumnHeaders;
public WorksheetRowTypeInfo<MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders> ClassWithInvalidPropertyReferenceColumnHeaders => _ClassWithInvalidPropertyReferenceColumnHeaders
??= WorksheetRowMetadataServices.CreateObjectInfo<MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders>(AddHeaderRow0Async, AddAsRowAsync, AddRangeAsRowsAsync);

private static async ValueTask AddHeaderRow0Async(SpreadCheetah.Spreadsheet spreadsheet, SpreadCheetah.Styling.StyleId? styleId, CancellationToken token)
{
var cells = ArrayPool<StyledCell>.Shared.Rent(7);
try
{
cells[0] = new StyledCell("PropertyA", styleId);
cells[1] = new StyledCell("PropertyB", styleId);
cells[2] = new StyledCell("PropertyC", styleId);
cells[3] = new StyledCell("PropertyD", styleId);
cells[4] = new StyledCell("PropertyE", styleId);
cells[5] = new StyledCell("PropertyF", styleId);
cells[6] = new StyledCell("PropertyG", styleId);
await spreadsheet.AddRowAsync(cells.AsMemory(0, 7), token).ConfigureAwait(false);
}
finally
{
ArrayPool<StyledCell>.Shared.Return(cells, true);
}
}

private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders? obj, CancellationToken token)
{
if (spreadsheet is null)
throw new ArgumentNullException(nameof(spreadsheet));
if (obj is null)
return spreadsheet.AddRowAsync(ReadOnlyMemory<DataCell>.Empty, token);
return AddAsRowInternalAsync(spreadsheet, obj, token);
}

private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders?> 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.ClassWithInvalidPropertyReferenceColumnHeaders obj, CancellationToken token)
{
var cells = ArrayPool<DataCell>.Shared.Rent(7);
try
{
await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false);
}
finally
{
ArrayPool<DataCell>.Shared.Return(cells, true);
}
}

private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders?> objs, CancellationToken token)
{
var cells = ArrayPool<DataCell>.Shared.Rent(7);
try
{
await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false);
}
finally
{
ArrayPool<DataCell>.Shared.Return(cells, true);
}
}

private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<MyNamespace.ClassWithInvalidPropertyReferenceColumnHeaders?> 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)
{
if (obj is null)
return spreadsheet.AddRowAsync(ReadOnlyMemory<DataCell>.Empty, token);

cells[0] = new DataCell(obj.PropertyA);
cells[1] = new DataCell(obj.PropertyB);
cells[2] = new DataCell(obj.PropertyC);
cells[3] = new DataCell(obj.PropertyD);
cells[4] = new DataCell(obj.PropertyE);
cells[5] = new DataCell(obj.PropertyF);
cells[6] = new DataCell(obj.PropertyG);
return spreadsheet.AddRowAsync(cells.AsMemory(0, 7), token);
}
}
}
Loading

0 comments on commit 66f5a6d

Please sign in to comment.