Skip to content

Commit

Permalink
Generate non-generic write logic with source generators (#3513)
Browse files Browse the repository at this point in the history
Part of #2940
Part of #3300
  • Loading branch information
roji authored Feb 8, 2021
1 parent be31084 commit ff8b794
Show file tree
Hide file tree
Showing 71 changed files with 402 additions and 420 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<ItemGroup>
<PackageReference Update="System.Runtime.CompilerServices.Unsafe" Version="4.6.0" />
<PackageReference Update="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
<PackageReference Update="System.Threading.Tasks.Extensions" Version="4.5.3" />
<PackageReference Update="System.Memory" Version="4.5.3" />
<PackageReference Update="System.ValueTuple" Version="4.5.0" />
Expand Down
11 changes: 11 additions & 0 deletions Npgsql.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Build.targets = Directory.Build.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Npgsql.SourceGenerators", "src\Npgsql.SourceGenerators\Npgsql.SourceGenerators.csproj", "{63026A19-60B8-4906-81CB-216F30E8094B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -123,6 +125,14 @@ Global
{A77E5FAF-D775-4AB4-8846-8965C2104E60}.Release|Any CPU.Build.0 = Release|Any CPU
{A77E5FAF-D775-4AB4-8846-8965C2104E60}.Release|x86.ActiveCfg = Release|Any CPU
{A77E5FAF-D775-4AB4-8846-8965C2104E60}.Release|x86.Build.0 = Release|Any CPU
{63026A19-60B8-4906-81CB-216F30E8094B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63026A19-60B8-4906-81CB-216F30E8094B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63026A19-60B8-4906-81CB-216F30E8094B}.Debug|x86.ActiveCfg = Debug|Any CPU
{63026A19-60B8-4906-81CB-216F30E8094B}.Debug|x86.Build.0 = Debug|Any CPU
{63026A19-60B8-4906-81CB-216F30E8094B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63026A19-60B8-4906-81CB-216F30E8094B}.Release|Any CPU.Build.0 = Release|Any CPU
{63026A19-60B8-4906-81CB-216F30E8094B}.Release|x86.ActiveCfg = Release|Any CPU
{63026A19-60B8-4906-81CB-216F30E8094B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -138,6 +148,7 @@ Global
{6CB12050-DC9B-4155-BADD-BFDD54CDD70F} = {8537E50E-CF7F-49CB-B4EF-3E2A1B11F050}
{F7C53EBD-0075-474F-A083-419257D04080} = {8537E50E-CF7F-49CB-B4EF-3E2A1B11F050}
{A77E5FAF-D775-4AB4-8846-8965C2104E60} = {ED612DB1-AB32-4603-95E7-891BACA71C39}
{63026A19-60B8-4906-81CB-216F30E8094B} = {8537E50E-CF7F-49CB-B4EF-3E2A1B11F050}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C90AEECD-DB4C-4BE6-B506-16A449852FB8}
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.GeoJSON/Internal/GeoJSONHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override NpgsqlTypeHandler<GeoJSONObject> Create(PostgresType postgresTyp
}
}

sealed class GeoJsonHandler : NpgsqlTypeHandler<GeoJSONObject>,
sealed partial class GeoJsonHandler : NpgsqlTypeHandler<GeoJSONObject>,
INpgsqlTypeHandler<Point>, INpgsqlTypeHandler<MultiPoint>,
INpgsqlTypeHandler<Polygon>, INpgsqlTypeHandler<MultiPolygon>,
INpgsqlTypeHandler<LineString>, INpgsqlTypeHandler<MultiLineString>,
Expand Down
3 changes: 3 additions & 0 deletions src/Npgsql.GeoJSON/Npgsql.GeoJSON.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Npgsql/Npgsql.csproj" />
<ProjectReference Include="../Npgsql.SourceGenerators/Npgsql.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions src/Npgsql.Json.NET/Internal/JsonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, Npg
return base.WriteWithLength(serialized, buf, lengthCache, parameter, async, cancellationToken);
}

protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
public override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{
if (value is DBNull ||
value is string ||
Expand All @@ -91,7 +91,7 @@ value is char ||
return ValidateAndGetLength(value, ref lengthCache, parameter);
}

protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default)
public override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default)
{
if (value is DBNull ||
value is string ||
Expand Down
4 changes: 2 additions & 2 deletions src/Npgsql.Json.NET/Internal/JsonbHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, Npg
return base.WriteWithLength(serialized, buf, lengthCache, parameter, async, cancellationToken);
}

protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
public override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{
if (value is DBNull ||
value is string ||
Expand All @@ -91,7 +91,7 @@ value is char ||
return ValidateAndGetLength(value, ref lengthCache, parameter);
}

protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default)
public override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default)
{
if (value is DBNull ||
value is string ||
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.LegacyPostgis/Internal/LegacyPostgisHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public override NpgsqlTypeHandler<PostgisGeometry> Create(PostgresType postgresT
=> new LegacyPostgisHandler(postgresType);
}

class LegacyPostgisHandler : NpgsqlTypeHandler<PostgisGeometry>,
partial class LegacyPostgisHandler : NpgsqlTypeHandler<PostgisGeometry>,
INpgsqlTypeHandler<PostgisPoint>, INpgsqlTypeHandler<PostgisMultiPoint>,
INpgsqlTypeHandler<PostgisLineString>, INpgsqlTypeHandler<PostgisMultiLineString>,
INpgsqlTypeHandler<PostgisPolygon>, INpgsqlTypeHandler<PostgisMultiPolygon>,
Expand Down
3 changes: 3 additions & 0 deletions src/Npgsql.LegacyPostgis/Npgsql.LegacyPostgis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Npgsql\Npgsql.csproj" />
<ProjectReference Include="../Npgsql.SourceGenerators/Npgsql.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Npgsql.NetTopologySuite.Internal
{
class NetTopologySuiteHandler : NpgsqlTypeHandler<Geometry>,
partial class NetTopologySuiteHandler : NpgsqlTypeHandler<Geometry>,
INpgsqlTypeHandler<Point>,
INpgsqlTypeHandler<LineString>,
INpgsqlTypeHandler<Polygon>,
Expand Down
3 changes: 3 additions & 0 deletions src/Npgsql.NetTopologySuite/Npgsql.NetTopologySuite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Npgsql/Npgsql.csproj" />
<ProjectReference Include="../Npgsql.SourceGenerators/Npgsql.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Npgsql.NodaTime/Internal/DateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override NpgsqlTypeHandler<LocalDate> Create(PostgresType postgresType, N
}
}

sealed class DateHandler : NpgsqlSimpleTypeHandler<LocalDate>, INpgsqlSimpleTypeHandler<DateTime>, INpgsqlSimpleTypeHandler<NpgsqlDate>
sealed partial class DateHandler : NpgsqlSimpleTypeHandler<LocalDate>, INpgsqlSimpleTypeHandler<DateTime>, INpgsqlSimpleTypeHandler<NpgsqlDate>
{
/// <summary>
/// Whether to convert positive and negative infinity values to Instant.{Max,Min}Value when
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.NodaTime/Internal/IntervalHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override NpgsqlTypeHandler<Period> Create(PostgresType postgresType, Npgs
: throw new NotSupportedException($"The deprecated floating-point date/time format is not supported by {nameof(Npgsql)}.");
}

sealed class IntervalHandler :
sealed partial class IntervalHandler :
NpgsqlSimpleTypeHandler<Period>,
INpgsqlSimpleTypeHandler<Duration>,
INpgsqlSimpleTypeHandler<NpgsqlTimeSpan>,
Expand Down
4 changes: 3 additions & 1 deletion src/Npgsql.NodaTime/Internal/TimeHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using NodaTime;
using Npgsql.BackendMessages;
using Npgsql.Internal;
Expand All @@ -17,7 +19,7 @@ public override NpgsqlTypeHandler<LocalTime> Create(PostgresType postgresType, N
: throw new NotSupportedException($"The deprecated floating-point date/time format is not supported by {nameof(Npgsql)}.");
}

sealed class TimeHandler : NpgsqlSimpleTypeHandler<LocalTime>, INpgsqlSimpleTypeHandler<TimeSpan>
sealed partial class TimeHandler : NpgsqlSimpleTypeHandler<LocalTime>, INpgsqlSimpleTypeHandler<TimeSpan>
{
readonly BclTimeHandler _bclHandler;

Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.NodaTime/Internal/TimeTzHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override NpgsqlTypeHandler<OffsetTime> Create(PostgresType postgresType,
: throw new NotSupportedException($"The deprecated floating-point date/time format is not supported by {nameof(Npgsql)}.");
}

sealed class TimeTzHandler : NpgsqlSimpleTypeHandler<OffsetTime>, INpgsqlSimpleTypeHandler<DateTimeOffset>,
sealed partial class TimeTzHandler : NpgsqlSimpleTypeHandler<OffsetTime>, INpgsqlSimpleTypeHandler<DateTimeOffset>,
INpgsqlSimpleTypeHandler<DateTime>, INpgsqlSimpleTypeHandler<TimeSpan>
{
readonly BclTimeTzHandler _bclHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.NodaTime/Internal/TimestampHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override NpgsqlTypeHandler<Instant> Create(PostgresType postgresType, Npg
}
}

sealed class TimestampHandler : NpgsqlSimpleTypeHandler<Instant>, INpgsqlSimpleTypeHandler<LocalDateTime>, INpgsqlSimpleTypeHandler<DateTime>
sealed partial class TimestampHandler : NpgsqlSimpleTypeHandler<Instant>, INpgsqlSimpleTypeHandler<LocalDateTime>, INpgsqlSimpleTypeHandler<DateTime>
{
static readonly Instant Instant0 = Instant.FromUtc(1, 1, 1, 0, 0, 0);
static readonly Instant Instant2000 = Instant.FromUtc(2000, 1, 1, 0, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.NodaTime/Internal/TimestampTzHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override NpgsqlTypeHandler<Instant> Create(PostgresType postgresType, Npg
}
}

sealed class TimestampTzHandler : NpgsqlSimpleTypeHandler<Instant>, INpgsqlSimpleTypeHandler<ZonedDateTime>,
sealed partial class TimestampTzHandler : NpgsqlSimpleTypeHandler<Instant>, INpgsqlSimpleTypeHandler<ZonedDateTime>,
INpgsqlSimpleTypeHandler<OffsetDateTime>, INpgsqlSimpleTypeHandler<DateTimeOffset>,
INpgsqlSimpleTypeHandler<DateTime>
{
Expand Down
3 changes: 3 additions & 0 deletions src/Npgsql.NodaTime/Npgsql.NodaTime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Npgsql\Npgsql.csproj" />
<ProjectReference Include="../Npgsql.SourceGenerators/Npgsql.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions src/Npgsql.SourceGenerators/EmbeddedResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.IO;
using System.Reflection;

namespace Npgsql.SourceGenerators
{
static class EmbeddedResource
{
public static string GetContent(string relativePath)
{
var baseName = Assembly.GetExecutingAssembly().GetName().Name;
var resourceName = relativePath
.TrimStart('.')
.Replace(Path.DirectorySeparatorChar, '.')
.Replace(Path.AltDirectorySeparatorChar, '.');

using var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(baseName + "." + resourceName);

if (stream == null)
throw new NotSupportedException();

using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
}
12 changes: 12 additions & 0 deletions src/Npgsql.SourceGenerators/Npgsql.SourceGenerators.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
</ItemGroup>
</Project>
Loading

0 comments on commit ff8b794

Please sign in to comment.