Skip to content

Commit

Permalink
Revert removal of templatized TValue dapper handler
Browse files Browse the repository at this point in the history
This breaks the Ulid scenario otherwise. Adapt test to ensure we're using this template in the test so we never drop it in the future.
  • Loading branch information
kzu committed Dec 22, 2024
1 parent 88814cc commit 24f281b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
26 changes: 1 addition & 25 deletions src/StructId.Tests/DapperGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,7 @@ public override void SetValue(IDbDataParameter parameter, System.Ulid value)
public readonly partial record struct UserId(Ulid Value): IStructId<Ulid>;
""",
"""
using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using StructId;
[TValue]
file class TValue_TypeHandler : Dapper.SqlMapper.TypeHandler<TValue>
{
public override TValue Parse(object value) => TValue.Parse((string)value, null);
public override void SetValue(IDbDataParameter parameter, TValue value)
{
parameter.DbType = DbType.String;
parameter.Value = value.ToString(null, null);
}
}
file partial struct TValue : IParsable<TValue>, IFormattable
{
public static TValue Parse(string s, IFormatProvider? provider) => throw new NotImplementedException();
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out TValue result) => throw new NotImplementedException();
public string ToString(string? format, IFormatProvider? formatProvider) => throw new NotImplementedException();
}
"""
ThisAssembly.Resources.StructId.Templates.DapperTypeHandler.Text
},
GeneratedSources =
{
Expand Down
27 changes: 27 additions & 0 deletions src/StructId/Templates/DapperTypeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <auto-generated />
#nullable enable

using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using StructId;

// TODO: pending making it conditionally included at compile-time
[TValue]
file class TValue_TypeHandler : Dapper.SqlMapper.TypeHandler<TValue>
{
public override TValue Parse(object value) => TValue.Parse((string)value, null);

public override void SetValue(IDbDataParameter parameter, TValue value)
{
parameter.DbType = DbType.String;
parameter.Value = value.ToString(null, null);
}
}

file partial struct TValue : IParsable<TValue>, IFormattable
{
public static TValue Parse(string s, IFormatProvider? provider) => throw new NotImplementedException();
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out TValue result) => throw new NotImplementedException();
public string ToString(string? format, IFormatProvider? formatProvider) => throw new NotImplementedException();
}

0 comments on commit 24f281b

Please sign in to comment.