Skip to content

Commit

Permalink
Support writing records with skipped fields in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkozlenko committed Dec 1, 2024
1 parent 31ceccf commit aa03d61
Show file tree
Hide file tree
Showing 28 changed files with 438 additions and 512 deletions.
3 changes: 1 addition & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project>
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0-3.final" />
<PackageVersion Include="MSTest" Version="3.6.3" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="MSTest" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<AnalyzerLanguage>cs</AnalyzerLanguage>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
75 changes: 0 additions & 75 deletions src/Addax.Formats.Tabular.Analyzers.CSharp/SourceTextBuilder.cs

This file was deleted.

72 changes: 72 additions & 0 deletions src/Addax.Formats.Tabular.Analyzers.CSharp/SourceTextWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// (c) Oleksandr Kozlenko. Licensed under the MIT license.

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using Microsoft.CodeAnalysis.Text;

namespace Addax.Formats.Tabular.Analyzers.CSharp
{
internal sealed class SourceTextWriter : IDisposable
{
private readonly MemoryStream _memoryStream = new MemoryStream();
private readonly StreamWriter _streamWriter;
private readonly char _indentChar;
private readonly int _indentSize;

public SourceTextWriter(Encoding encoding, char indentChar, int indentSize)
{
Debug.Assert(encoding != null);
Debug.Assert(indentSize >= 0);

_streamWriter = new StreamWriter(_memoryStream, encoding);
_indentChar = indentChar;
_indentSize = indentSize;
}

public void Dispose()
{
_streamWriter.Dispose();
}

public void WriteLine(string value)
{
Debug.Assert(value != null);

var indentation = Indent * _indentSize;

for (var i = 0; i < indentation; i++)
{
_streamWriter.Write(_indentChar);
}

_streamWriter.WriteLine(value);
}

public void WriteLine()
{
_streamWriter.WriteLine();
}

public SourceText ToSourceText()
{
_streamWriter.Flush();

return SourceText.From(_memoryStream, _streamWriter.Encoding, canBeEmbedded: true);
}

public void Reset()
{
Indent = 0;

_memoryStream.SetLength(0);
}

public int Indent
{
get;
set;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ internal readonly struct TabularFieldMapping
public readonly string MemberName;
public readonly string ValueTypeName;
public readonly string ConverterTypeName;
public readonly TypeMemberAccess MemberAccess;
public readonly bool AsNullableT;
public readonly bool SupportsWriting;
public readonly bool SupportsReading;
public readonly bool IsNullableT;

public TabularFieldMapping(
string memberName,
TypeMemberAccess memberAccess,
bool asNullableT,
bool supportsReading,
bool supportsWriting,
bool isNullableT,
string valueTypeName,
string converterTypeName,
SyntaxToken? fieldNameLiteral)
Expand All @@ -26,8 +28,9 @@ public TabularFieldMapping(
Debug.Assert(valueTypeName != null);

MemberName = memberName;
MemberAccess = memberAccess;
AsNullableT = asNullableT;
SupportsReading = supportsReading;
SupportsWriting = supportsWriting;
IsNullableT = isNullableT;
ValueTypeName = valueTypeName;
ConverterTypeName = converterTypeName;
FieldNameLiteral = fieldNameLiteral;
Expand Down
Loading

0 comments on commit aa03d61

Please sign in to comment.