diff --git a/StronglyTypedId.sln b/StronglyTypedId.sln index 2056da679..490732e60 100644 --- a/StronglyTypedId.sln +++ b/StronglyTypedId.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.28803.352 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32728.150 MinimumVisualStudioVersion = 15.0.26124.0 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EE1258BD-3422-4F55-B9CF-B4D6C95DAD68}" EndProject @@ -15,19 +15,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution version.props = version.props EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Tests", "test\StronglyTypedIds.Tests\StronglyTypedIds.Tests.csproj", "{00B5ED3F-827D-41CD-9AF2-A9A20A6604E1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Tests", "test\StronglyTypedIds.Tests\StronglyTypedIds.Tests.csproj", "{00B5ED3F-827D-41CD-9AF2-A9A20A6604E1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.IntegrationTests", "test\StronglyTypedIds.IntegrationTests\StronglyTypedIds.IntegrationTests.csproj", "{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.IntegrationTests", "test\StronglyTypedIds.IntegrationTests\StronglyTypedIds.IntegrationTests.csproj", "{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "build\_build.csproj", "{E13FB452-2D47-4719-8BAA-7B695D79AF3A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Attributes", "src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj", "{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Attributes", "src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj", "{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Nuget.IntegrationTests", "test\StronglyTypedIds.Nuget.IntegrationTests\StronglyTypedIds.Nuget.IntegrationTests.csproj", "{A7355210-7DDC-4968-84B7-79002113EA6E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Nuget.IntegrationTests", "test\StronglyTypedIds.Nuget.IntegrationTests\StronglyTypedIds.Nuget.IntegrationTests.csproj", "{A7355210-7DDC-4968-84B7-79002113EA6E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Nuget.Attributes.IntegrationTests", "test\StronglyTypedIds.Nuget.Attributes.IntegrationTests\StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj", "{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Nuget.Attributes.IntegrationTests", "test\StronglyTypedIds.Nuget.Attributes.IntegrationTests\StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj", "{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,8 +39,6 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|Any CPU.ActiveCfg = Release|Any CPU {9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|Any CPU.Build.0 = Debug|Any CPU {9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -77,6 +75,16 @@ Global {09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x64.Build.0 = Release|Any CPU {09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x86.ActiveCfg = Release|Any CPU {09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x86.Build.0 = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x64.ActiveCfg = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x64.Build.0 = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x86.ActiveCfg = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x86.Build.0 = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x64.ActiveCfg = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x64.Build.0 = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x86.ActiveCfg = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x86.Build.0 = Release|Any CPU {F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|Any CPU.Build.0 = Debug|Any CPU {F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|x64.ActiveCfg = Debug|Any CPU diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs index 0016511b6..398ae8e0b 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs @@ -11,12 +11,14 @@ public enum StronglyTypedIdBackingType /// Use the default backing type (either the globally configured default, or Guid) /// Default = 0, - + Guid = 1, Int = 2, String = 3, Long = 4, NullableString = 5, MassTransitNewId = 6, + Short = 7, + Byte = 8, } } \ No newline at end of file diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs index e1f229779..ffadda4e6 100644 --- a/src/StronglyTypedIds/EmbeddedSources.cs +++ b/src/StronglyTypedIds/EmbeddedSources.cs @@ -28,6 +28,30 @@ internal static class EmbeddedSources false ); + internal static readonly ResourceCollection ByteResources = new( + AutoGeneratedHeader, + LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_Base.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_NewtonsoftJsonConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_SystemTextJsonConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_TypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_EfCoreValueConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_DapperTypeHandler.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_IComparable.cs"), + false + ); + + internal static readonly ResourceCollection ShortResources = new( + AutoGeneratedHeader, + LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_Base.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_NewtonsoftJsonConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_SystemTextJsonConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_TypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_EfCoreValueConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_DapperTypeHandler.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_IComparable.cs"), + false + ); + internal static readonly ResourceCollection IntResources = new( AutoGeneratedHeader, LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Base.cs"), diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs index e33515310..6c59d08db 100644 --- a/src/StronglyTypedIds/SourceGenerationHelper.cs +++ b/src/StronglyTypedIds/SourceGenerationHelper.cs @@ -32,6 +32,8 @@ public static string CreateId( StronglyTypedIdBackingType.String => EmbeddedSources.StringResources, StronglyTypedIdBackingType.NullableString => EmbeddedSources.NullableStringResources, StronglyTypedIdBackingType.MassTransitNewId => EmbeddedSources.NewIdResources, + StronglyTypedIdBackingType.Short => EmbeddedSources.ShortResources, + StronglyTypedIdBackingType.Byte => EmbeddedSources.ByteResources, _ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)), }; diff --git a/src/StronglyTypedIds/Templates/Byte/Byte_Base.cs b/src/StronglyTypedIds/Templates/Byte/Byte_Base.cs new file mode 100644 index 000000000..cf14f1e9c --- /dev/null +++ b/src/StronglyTypedIds/Templates/Byte/Byte_Base.cs @@ -0,0 +1,23 @@ + readonly partial struct TESTID : INTERFACES + { + public byte Value { get; } + + public TESTID(byte value) + { + Value = value; + } + + public static readonly TESTID Empty = new TESTID(0); + + public bool Equals(TESTID other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is TESTID other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); + public static bool operator !=(TESTID a, TESTID b) => !(a == b); diff --git a/src/StronglyTypedIds/Templates/Byte/Byte_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Byte/Byte_DapperTypeHandler.cs new file mode 100644 index 000000000..55a42f05c --- /dev/null +++ b/src/StronglyTypedIds/Templates/Byte/Byte_DapperTypeHandler.cs @@ -0,0 +1,21 @@ + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) + { + parameter.Value = value.Value; + } + + public override TESTID Parse(object value) + { + return value switch + { + byte byteValue => new TESTID(byteValue), + short shortValue when shortValue < byte.MaxValue => new TESTID((byte)shortValue), + int intValue when intValue < byte.MaxValue => new TESTID((byte)intValue), + long longValue when longValue < byte.MaxValue => new TESTID((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new TESTID(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), + }; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Byte/Byte_EfCoreValueConverter.cs b/src/StronglyTypedIds/Templates/Byte/Byte_EfCoreValueConverter.cs new file mode 100644 index 000000000..127e5997d --- /dev/null +++ b/src/StronglyTypedIds/Templates/Byte/Byte_EfCoreValueConverter.cs @@ -0,0 +1,12 @@ + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new TESTID(value), + mappingHints + ) + { } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Byte/Byte_IComparable.cs b/src/StronglyTypedIds/Templates/Byte/Byte_IComparable.cs new file mode 100644 index 000000000..e0696ee2a --- /dev/null +++ b/src/StronglyTypedIds/Templates/Byte/Byte_IComparable.cs @@ -0,0 +1 @@ + public int CompareTo(TESTID other) => Value.CompareTo(other.Value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Byte/Byte_NewtonsoftJsonConverter.cs b/src/StronglyTypedIds/Templates/Byte/Byte_NewtonsoftJsonConverter.cs new file mode 100644 index 000000000..1f6152072 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Byte/Byte_NewtonsoftJsonConverter.cs @@ -0,0 +1,20 @@ + + class TESTIDNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(TESTID); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (TESTID)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new TESTID(result.Value) : null; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Byte/Byte_SystemTextJsonConverter.cs b/src/StronglyTypedIds/Templates/Byte/Byte_SystemTextJsonConverter.cs new file mode 100644 index 000000000..e09aeab19 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Byte/Byte_SystemTextJsonConverter.cs @@ -0,0 +1,13 @@ + + class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new TESTID(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Byte/Byte_TypeConverter.cs b/src/StronglyTypedIds/Templates/Byte/Byte_TypeConverter.cs new file mode 100644 index 000000000..baab01357 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Byte/Byte_TypeConverter.cs @@ -0,0 +1,41 @@ + + class TESTIDTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new TESTID(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new TESTID(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is TESTID idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Short/Short_Base.cs b/src/StronglyTypedIds/Templates/Short/Short_Base.cs new file mode 100644 index 000000000..8a312eb32 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Short/Short_Base.cs @@ -0,0 +1,23 @@ + readonly partial struct TESTID : INTERFACES + { + public short Value { get; } + + public TESTID(short value) + { + Value = value; + } + + public static readonly TESTID Empty = new TESTID(0); + + public bool Equals(TESTID other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is TESTID other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); + public static bool operator !=(TESTID a, TESTID b) => !(a == b); diff --git a/src/StronglyTypedIds/Templates/Short/Short_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Short/Short_DapperTypeHandler.cs new file mode 100644 index 000000000..10809a97f --- /dev/null +++ b/src/StronglyTypedIds/Templates/Short/Short_DapperTypeHandler.cs @@ -0,0 +1,21 @@ + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) + { + parameter.Value = value.Value; + } + + public override TESTID Parse(object value) + { + return value switch + { + short shortValue => new TESTID(shortValue), + byte byteValue => new TESTID(byteValue), + int intValue when intValue < byte.MaxValue => new TESTID((short)intValue), + long longValue when longValue < byte.MaxValue => new TESTID((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new TESTID(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), + }; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Short/Short_EfCoreValueConverter.cs b/src/StronglyTypedIds/Templates/Short/Short_EfCoreValueConverter.cs new file mode 100644 index 000000000..a4aa99c34 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Short/Short_EfCoreValueConverter.cs @@ -0,0 +1,12 @@ + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new TESTID(value), + mappingHints + ) + { } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Short/Short_IComparable.cs b/src/StronglyTypedIds/Templates/Short/Short_IComparable.cs new file mode 100644 index 000000000..e0696ee2a --- /dev/null +++ b/src/StronglyTypedIds/Templates/Short/Short_IComparable.cs @@ -0,0 +1 @@ + public int CompareTo(TESTID other) => Value.CompareTo(other.Value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Short/Short_NewtonsoftJsonConverter.cs b/src/StronglyTypedIds/Templates/Short/Short_NewtonsoftJsonConverter.cs new file mode 100644 index 000000000..c9901f16e --- /dev/null +++ b/src/StronglyTypedIds/Templates/Short/Short_NewtonsoftJsonConverter.cs @@ -0,0 +1,20 @@ + + class TESTIDNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(TESTID); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (TESTID)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new TESTID(result.Value) : null; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Short/Short_SystemTextJsonConverter.cs b/src/StronglyTypedIds/Templates/Short/Short_SystemTextJsonConverter.cs new file mode 100644 index 000000000..5b791b80f --- /dev/null +++ b/src/StronglyTypedIds/Templates/Short/Short_SystemTextJsonConverter.cs @@ -0,0 +1,13 @@ + + class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new TESTID(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Short/Short_TypeConverter.cs b/src/StronglyTypedIds/Templates/Short/Short_TypeConverter.cs new file mode 100644 index 000000000..677747e58 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Short/Short_TypeConverter.cs @@ -0,0 +1,41 @@ + + class TESTIDTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new TESTID(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new TESTID(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is TESTID idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/ByteIdTests.cs b/test/StronglyTypedIds.IntegrationTests/ByteIdTests.cs new file mode 100644 index 000000000..96c4e036f --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/ByteIdTests.cs @@ -0,0 +1,352 @@ +using System; +using System.ComponentModel; +using System.Linq; +using System.Threading.Tasks; +using Dapper; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using StronglyTypedIds.IntegrationTests.Types; +using Xunit; +using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; +using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; + +namespace StronglyTypedIds.IntegrationTests +{ + public class ByteIdTests + { + [Fact] + public void SameValuesAreEqual() + { + byte id = 123; + var foo1 = new ByteId(id); + var foo2 = new ByteId(id); + + Assert.Equal(foo1, foo2); + } + + [Fact] + public void EmptyValueIsEmpty() + { + Assert.Equal(0, ByteId.Empty.Value); + } + + + [Fact] + public void DifferentValuesAreUnequal() + { + var foo1 = new ByteId(1); + var foo2 = new ByteId(2); + + Assert.NotEqual(foo1, foo2); + } + + [Fact] + public void OverloadsWorkCorrectly() + { + byte id = 12; + var same1 = new ByteId(id); + var same2 = new ByteId(id); + var different = new ByteId(3); + + Assert.True(same1 == same2); + Assert.False(same1 == different); + Assert.False(same1 != same2); + Assert.True(same1 != different); + } + + [Fact] + public void DifferentTypesAreUnequal() + { + var bar = GuidId2.New(); + var foo = new ByteId(23); + + //Assert.NotEqual(bar, foo); // does not compile + Assert.NotEqual((object)bar, (object)foo); + } + + [Fact] + public void CanSerializeToInt_WithNewtonsoftJsonProvider() + { + var foo = new NewtonsoftJsonByteId(123); + + var serializedFoo = NewtonsoftJsonSerializer.SerializeObject(foo); + var serializedInt = NewtonsoftJsonSerializer.SerializeObject(foo.Value); + + Assert.Equal(serializedFoo, serializedInt); + } + + [Fact] + public void CanSerializeToNullableInt_WithNewtonsoftJsonProvider() + { + var entity = new EntityWithNullableId { Id = null }; + + var json = NewtonsoftJsonSerializer.SerializeObject(entity); + var deserialize = NewtonsoftJsonSerializer.DeserializeObject(json); + + Assert.NotNull(deserialize); + Assert.Null(deserialize.Id); + } + + [Fact] + public void CanSerializeToInt_WithSystemTextJsonProvider() + { + var foo = new SystemTextJsonByteId(123); + + var serializedFoo = SystemTextJsonSerializer.Serialize(foo); + var serializedInt = SystemTextJsonSerializer.Serialize(foo.Value); + + Assert.Equal(serializedFoo, serializedInt); + } + + [Fact] + public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() + { + byte value = 123; + var foo = new NewtonsoftJsonByteId(value); + var serializedInt = NewtonsoftJsonSerializer.SerializeObject(value); + + var deserializedFoo = NewtonsoftJsonSerializer.DeserializeObject(serializedInt); + + Assert.Equal(foo, deserializedFoo); + } + + [Fact] + public void CanDeserializeFromInt_WithSystemTextJsonProvider() + { + byte value = 123; + var foo = new SystemTextJsonByteId(value); + var serializedInt = SystemTextJsonSerializer.Serialize(value); + + var deserializedFoo = SystemTextJsonSerializer.Deserialize(serializedInt); + + Assert.Equal(foo, deserializedFoo); + } + + [Fact] + public void CanSerializeToInt_WithBothJsonConverters() + { + var foo = new BothJsonByteId(123); + + var serializedFoo1 = NewtonsoftJsonSerializer.SerializeObject(foo); + var serializedInt1 = NewtonsoftJsonSerializer.SerializeObject(foo.Value); + + var serializedFoo2 = SystemTextJsonSerializer.Serialize(foo); + var serializedInt2 = SystemTextJsonSerializer.Serialize(foo.Value); + + Assert.Equal(serializedFoo1, serializedInt1); + Assert.Equal(serializedFoo2, serializedInt2); + } + + [Fact] + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + { + var foo = new NoJsonByteId(123); + + var serialized = SystemTextJsonSerializer.Serialize(foo); + + var expected = "{\"Value\":" + foo.Value + "}"; + + Assert.Equal(expected, serialized); + } + + [Fact] + public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() + { + var foo = new NoJsonByteId(123); + + var serialized = NewtonsoftJsonSerializer.SerializeObject(foo); + + var expected = $"\"{foo.Value}\""; + + Assert.Equal(expected, serialized); + } + + [Fact] + public void WhenNoTypeConverter_SerializesWithValueProperty() + { + var foo = new NoConverterByteId(123); + + var newtonsoft = SystemTextJsonSerializer.Serialize(foo); + var systemText = SystemTextJsonSerializer.Serialize(foo); + + var expected = "{\"Value\":" + foo.Value + "}"; + + Assert.Equal(expected, newtonsoft); + Assert.Equal(expected, systemText); + } + + [Fact] + public void WhenEfCoreValueConverterUsesValueConverter() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + var original = new TestEntity { Id = new EfCoreByteId(123) }; + using (var context = new TestDbContext(options)) + { + context.Database.EnsureCreated(); + context.Entities.Add(original); + context.SaveChanges(); + } + using (var context = new TestDbContext(options)) + { + var all = context.Entities.ToList(); + var retrieved = Assert.Single(all); + Assert.Equal(original.Id, retrieved.Id); + } + } + + [Fact] + public async Task WhenDapperValueConverterUsesValueConverter() + { + using var connection = new SqliteConnection("DataSource=:memory:"); + await connection.OpenAsync(); + + var results = await connection.QueryAsync("SELECT 123"); + + var value = Assert.Single(results); + Assert.Equal(new DapperByteId(123), value); + } + + [Theory] + [InlineData((byte)123)] + [InlineData("123")] + public void TypeConverter_CanConvertToAndFrom(object value) + { + var converter = TypeDescriptor.GetConverter(typeof(NoJsonByteId)); + var id = converter.ConvertFrom(value); + Assert.IsType(id); + Assert.Equal(new NoJsonByteId(123), id); + + var reconverted = converter.ConvertTo(id, value.GetType()); + Assert.Equal(value, reconverted); + } + + [Fact] + public void CanCompareDefaults() + { + ComparableByteId original = default; + var other = ComparableByteId.Empty; + + var compare1 = original.CompareTo(other); + var compare2 = other.CompareTo(original); + Assert.Equal(compare1, -compare2); + } + + [Fact] + public void CanEquateDefaults() + { + EquatableByteId original = default; + var other = EquatableByteId.Empty; + + var equals1 = (original as IEquatable).Equals(other); + var equals2 = (other as IEquatable).Equals(original); + + Assert.Equal(equals1, equals2); + } + + [Fact] + public void ImplementsInterfaces() + { + Assert.IsAssignableFrom>(BothByteId.Empty); + Assert.IsAssignableFrom>(BothByteId.Empty); + + Assert.IsAssignableFrom>(EquatableByteId.Empty); + Assert.IsAssignableFrom>(ComparableByteId.Empty); + +#pragma warning disable 184 + Assert.False(ByteId.Empty is IComparable); + Assert.False(ByteId.Empty is IEquatable); +#pragma warning restore 184 + } + + +#if NET6_0_OR_GREATER + [Fact] + public void WhenConventionBasedEfCoreValueConverterUsesValueConverter() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new ConventionsDbContext(options)) + { + context.Database.EnsureCreated(); + context.Entities.Add( + new TestEntity { Id = new EfCoreByteId(123) }); + context.SaveChanges(); + } + using (var context = new ConventionsDbContext(options)) + { + var all = context.Entities.ToList(); + Assert.Single(all); + } + } + + public class ConventionsDbContext : DbContext + { + public DbSet Entities { get; set; } + + public ConventionsDbContext(DbContextOptions options) : base(options) + { + } + + protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) + { + configurationBuilder + .Properties() + .HaveConversion(); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder + .Entity(builder => + { + builder + .Property(x => x.Id) + .ValueGeneratedNever(); + }); + } + } +#endif + + public class TestDbContext : DbContext + { + public DbSet Entities { get; set; } + + public TestDbContext(DbContextOptions options) : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder + .Entity(builder => + { + builder + .Property(x => x.Id) + .HasConversion(new EfCoreByteId.EfCoreValueConverter()) + .ValueGeneratedNever(); + }); + } + } + + public class TestEntity + { + public EfCoreByteId Id { get; set; } + } + + public class EntityWithNullableId + { + public NewtonsoftJsonByteId? Id { get; set; } + } + } +} \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs b/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs index 0ce1ce679..8501d8926 100644 --- a/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs +++ b/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs @@ -12,6 +12,8 @@ public static void AddHandlers() SqlMapper.AddTypeHandler(new DapperGuidId.DapperTypeHandler()); SqlMapper.AddTypeHandler(new DapperIntId.DapperTypeHandler()); SqlMapper.AddTypeHandler(new DapperStringId.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new DapperByteId.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new DapperShortId.DapperTypeHandler()); SqlMapper.AddTypeHandler(new DapperLongId.DapperTypeHandler()); SqlMapper.AddTypeHandler(new DapperNullableStringId.DapperTypeHandler()); SqlMapper.AddTypeHandler(new DapperNewIdId.DapperTypeHandler()); diff --git a/test/StronglyTypedIds.IntegrationTests/ShortIdTests.cs b/test/StronglyTypedIds.IntegrationTests/ShortIdTests.cs new file mode 100644 index 000000000..8b48f5c14 --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/ShortIdTests.cs @@ -0,0 +1,352 @@ +using System; +using System.ComponentModel; +using System.Linq; +using System.Threading.Tasks; +using Dapper; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using StronglyTypedIds.IntegrationTests.Types; +using Xunit; +using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; +using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; + +namespace StronglyTypedIds.IntegrationTests +{ + public class ShortIdTests + { + [Fact] + public void SameValuesAreEqual() + { + short id = 123; + var foo1 = new ShortId(id); + var foo2 = new ShortId(id); + + Assert.Equal(foo1, foo2); + } + + [Fact] + public void EmptyValueIsEmpty() + { + Assert.Equal(0, ShortId.Empty.Value); + } + + + [Fact] + public void DifferentValuesAreUnequal() + { + var foo1 = new ShortId(1); + var foo2 = new ShortId(2); + + Assert.NotEqual(foo1, foo2); + } + + [Fact] + public void OverloadsWorkCorrectly() + { + short id = 12; + var same1 = new ShortId(id); + var same2 = new ShortId(id); + var different = new ShortId(3); + + Assert.True(same1 == same2); + Assert.False(same1 == different); + Assert.False(same1 != same2); + Assert.True(same1 != different); + } + + [Fact] + public void DifferentTypesAreUnequal() + { + var bar = GuidId2.New(); + var foo = new ShortId(23); + + //Assert.NotEqual(bar, foo); // does not compile + Assert.NotEqual((object)bar, (object)foo); + } + + [Fact] + public void CanSerializeToInt_WithNewtonsoftJsonProvider() + { + var foo = new NewtonsoftJsonShortId(123); + + var serializedFoo = NewtonsoftJsonSerializer.SerializeObject(foo); + var serializedInt = NewtonsoftJsonSerializer.SerializeObject(foo.Value); + + Assert.Equal(serializedFoo, serializedInt); + } + + [Fact] + public void CanSerializeToNullableInt_WithNewtonsoftJsonProvider() + { + var entity = new EntityWithNullableId { Id = null }; + + var json = NewtonsoftJsonSerializer.SerializeObject(entity); + var deserialize = NewtonsoftJsonSerializer.DeserializeObject(json); + + Assert.NotNull(deserialize); + Assert.Null(deserialize.Id); + } + + [Fact] + public void CanSerializeToInt_WithSystemTextJsonProvider() + { + var foo = new SystemTextJsonShortId(123); + + var serializedFoo = SystemTextJsonSerializer.Serialize(foo); + var serializedInt = SystemTextJsonSerializer.Serialize(foo.Value); + + Assert.Equal(serializedFoo, serializedInt); + } + + [Fact] + public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() + { + short value = 123; + var foo = new NewtonsoftJsonShortId(value); + var serializedInt = NewtonsoftJsonSerializer.SerializeObject(value); + + var deserializedFoo = NewtonsoftJsonSerializer.DeserializeObject(serializedInt); + + Assert.Equal(foo, deserializedFoo); + } + + [Fact] + public void CanDeserializeFromInt_WithSystemTextJsonProvider() + { + short value = 123; + var foo = new SystemTextJsonShortId(value); + var serializedInt = SystemTextJsonSerializer.Serialize(value); + + var deserializedFoo = SystemTextJsonSerializer.Deserialize(serializedInt); + + Assert.Equal(foo, deserializedFoo); + } + + [Fact] + public void CanSerializeToInt_WithBothJsonConverters() + { + var foo = new BothJsonShortId(123); + + var serializedFoo1 = NewtonsoftJsonSerializer.SerializeObject(foo); + var serializedInt1 = NewtonsoftJsonSerializer.SerializeObject(foo.Value); + + var serializedFoo2 = SystemTextJsonSerializer.Serialize(foo); + var serializedInt2 = SystemTextJsonSerializer.Serialize(foo.Value); + + Assert.Equal(serializedFoo1, serializedInt1); + Assert.Equal(serializedFoo2, serializedInt2); + } + + [Fact] + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + { + var foo = new NoJsonShortId(123); + + var serialized = SystemTextJsonSerializer.Serialize(foo); + + var expected = "{\"Value\":" + foo.Value + "}"; + + Assert.Equal(expected, serialized); + } + + [Fact] + public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() + { + var foo = new NoJsonShortId(123); + + var serialized = NewtonsoftJsonSerializer.SerializeObject(foo); + + var expected = $"\"{foo.Value}\""; + + Assert.Equal(expected, serialized); + } + + [Fact] + public void WhenNoTypeConverter_SerializesWithValueProperty() + { + var foo = new NoConverterShortId(123); + + var newtonsoft = SystemTextJsonSerializer.Serialize(foo); + var systemText = SystemTextJsonSerializer.Serialize(foo); + + var expected = "{\"Value\":" + foo.Value + "}"; + + Assert.Equal(expected, newtonsoft); + Assert.Equal(expected, systemText); + } + + [Fact] + public void WhenEfCoreValueConverterUsesValueConverter() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + var original = new TestEntity { Id = new EfCoreShortId(123) }; + using (var context = new TestDbContext(options)) + { + context.Database.EnsureCreated(); + context.Entities.Add(original); + context.SaveChanges(); + } + using (var context = new TestDbContext(options)) + { + var all = context.Entities.ToList(); + var retrieved = Assert.Single(all); + Assert.Equal(original.Id, retrieved.Id); + } + } + + [Fact] + public async Task WhenDapperValueConverterUsesValueConverter() + { + using var connection = new SqliteConnection("DataSource=:memory:"); + await connection.OpenAsync(); + + var results = await connection.QueryAsync("SELECT 123"); + + var value = Assert.Single(results); + Assert.Equal(new DapperShortId(123), value); + } + + [Theory] + [InlineData((short)123)] + [InlineData("123")] + public void TypeConverter_CanConvertToAndFrom(object value) + { + var converter = TypeDescriptor.GetConverter(typeof(NoJsonShortId)); + var id = converter.ConvertFrom(value); + Assert.IsType(id); + Assert.Equal(new NoJsonShortId(123), id); + + var reconverted = converter.ConvertTo(id, value.GetType()); + Assert.Equal(value, reconverted); + } + + [Fact] + public void CanCompareDefaults() + { + ComparableShortId original = default; + var other = ComparableShortId.Empty; + + var compare1 = original.CompareTo(other); + var compare2 = other.CompareTo(original); + Assert.Equal(compare1, -compare2); + } + + [Fact] + public void CanEquateDefaults() + { + EquatableShortId original = default; + var other = EquatableShortId.Empty; + + var equals1 = (original as IEquatable).Equals(other); + var equals2 = (other as IEquatable).Equals(original); + + Assert.Equal(equals1, equals2); + } + + [Fact] + public void ImplementsInterfaces() + { + Assert.IsAssignableFrom>(BothShortId.Empty); + Assert.IsAssignableFrom>(BothShortId.Empty); + + Assert.IsAssignableFrom>(EquatableShortId.Empty); + Assert.IsAssignableFrom>(ComparableShortId.Empty); + +#pragma warning disable 184 + Assert.False(ShortId.Empty is IComparable); + Assert.False(ShortId.Empty is IEquatable); +#pragma warning restore 184 + } + + +#if NET6_0_OR_GREATER + [Fact] + public void WhenConventionBasedEfCoreValueConverterUsesValueConverter() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new ConventionsDbContext(options)) + { + context.Database.EnsureCreated(); + context.Entities.Add( + new TestEntity { Id = new EfCoreShortId(123) }); + context.SaveChanges(); + } + using (var context = new ConventionsDbContext(options)) + { + var all = context.Entities.ToList(); + Assert.Single(all); + } + } + + public class ConventionsDbContext : DbContext + { + public DbSet Entities { get; set; } + + public ConventionsDbContext(DbContextOptions options) : base(options) + { + } + + protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) + { + configurationBuilder + .Properties() + .HaveConversion(); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder + .Entity(builder => + { + builder + .Property(x => x.Id) + .ValueGeneratedNever(); + }); + } + } +#endif + + public class TestDbContext : DbContext + { + public DbSet Entities { get; set; } + + public TestDbContext(DbContextOptions options) : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder + .Entity(builder => + { + builder + .Property(x => x.Id) + .HasConversion(new EfCoreShortId.EfCoreValueConverter()) + .ValueGeneratedNever(); + }); + } + } + + public class TestEntity + { + public EfCoreShortId Id { get; set; } + } + + public class EntityWithNullableId + { + public NewtonsoftJsonShortId? Id { get; set; } + } + } +} \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/ByteId.cs b/test/StronglyTypedIds.IntegrationTests/Types/ByteId.cs new file mode 100644 index 000000000..68448450e --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/Types/ByteId.cs @@ -0,0 +1,35 @@ +namespace StronglyTypedIds.IntegrationTests.Types +{ + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Byte)] + partial struct ByteId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.None, backingType: StronglyTypedIdBackingType.Byte)] + public partial struct NoConverterByteId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.TypeConverter, backingType: StronglyTypedIdBackingType.Byte)] + public partial struct NoJsonByteId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.NewtonsoftJson, backingType: StronglyTypedIdBackingType.Byte)] + public partial struct NewtonsoftJsonByteId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.SystemTextJson, backingType: StronglyTypedIdBackingType.Byte)] + public partial struct SystemTextJsonByteId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.NewtonsoftJson | StronglyTypedIdConverter.SystemTextJson, backingType: StronglyTypedIdBackingType.Byte)] + public partial struct BothJsonByteId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.EfCoreValueConverter, backingType: StronglyTypedIdBackingType.Byte)] + public partial struct EfCoreByteId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.Byte)] + public partial struct DapperByteId { } + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Byte, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] + public partial struct BothByteId { } + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Byte, implementations: StronglyTypedIdImplementations.IEquatable)] + public partial struct EquatableByteId { } + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Byte, implementations: StronglyTypedIdImplementations.IComparable)] + public partial struct ComparableByteId { } +} diff --git a/test/StronglyTypedIds.IntegrationTests/Types/ShortId.cs b/test/StronglyTypedIds.IntegrationTests/Types/ShortId.cs new file mode 100644 index 000000000..d869ea440 --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/Types/ShortId.cs @@ -0,0 +1,35 @@ +namespace StronglyTypedIds.IntegrationTests.Types +{ + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Short)] + partial struct ShortId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.None, backingType: StronglyTypedIdBackingType.Short)] + public partial struct NoConverterShortId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.TypeConverter, backingType: StronglyTypedIdBackingType.Short)] + public partial struct NoJsonShortId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.NewtonsoftJson, backingType: StronglyTypedIdBackingType.Short)] + public partial struct NewtonsoftJsonShortId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.SystemTextJson, backingType: StronglyTypedIdBackingType.Short)] + public partial struct SystemTextJsonShortId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.NewtonsoftJson | StronglyTypedIdConverter.SystemTextJson, backingType: StronglyTypedIdBackingType.Short)] + public partial struct BothJsonShortId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.EfCoreValueConverter, backingType: StronglyTypedIdBackingType.Short)] + public partial struct EfCoreShortId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.Short)] + public partial struct DapperShortId { } + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Short, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] + public partial struct BothShortId { } + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Short, implementations: StronglyTypedIdImplementations.IEquatable)] + public partial struct EquatableShortId { } + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Short, implementations: StronglyTypedIdImplementations.IComparable)] + public partial struct ComparableShortId { } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt index 4ea1be319..e185e0b07 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt @@ -24,13 +24,15 @@ namespace StronglyTypedIds /// Use the default backing type (either the globally configured default, or Guid) /// Default = 0, - + Guid = 1, Int = 2, String = 3, Long = 4, NullableString = 5, MassTransitNewId = 6, + Short = 7, + Byte = 8, } } #endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..d16468cfa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..8ef44c622 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..b2265600d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..5a23d23bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..15eadf29f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..5b239e49f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..a5e1baf1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..2b9296fd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4b41ca615 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..5256add24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..350717786 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..8a82544d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..9e472a668 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..424e53178 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..e2b69bdb4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..524453af1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..94b7baa22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..ecdd791a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..d43344f8d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..9bf32b58f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..5fbf07f9d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..0b396b8ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..5e1bfe560 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..6fe0f6240 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..428f3b0de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..ab5505958 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..a1ae6d289 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..d84c66835 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..7752a6ac8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..750fca9da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..30b149dd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..57bfc8255 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..2c41e35b3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..66858f50f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..4208ae86f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..e6cd49119 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..55de1a237 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..194c0c2dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..bc7b831d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..c0b1b0590 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4b91ae7e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..b4355839e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..f5b8c382d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..35c5858f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..de85a3d77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..4f789d174 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..a46378860 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..7276580f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..10e18135e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..d2ce3b2a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..e9f96edc4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..7a62e8713 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..b38171c6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..691f620b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..7a12ac68e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..ca8baa87d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..8da41ccce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..37daab286 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..efde4cfd0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..c42e94139 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..c80591968 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..9cff6186b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..df9cfcb5d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..67090b65a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..458946d49 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..e51cfea87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..c23266f07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,65 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..be4957ce4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,65 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..c211ad22b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..ec51728a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..0b6942ead --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,65 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..4211256e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,65 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..805898888 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..1a39c6521 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..6fdc454ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..9cf55e611 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..aa9b5a3a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..285a226f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..6a9344667 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..ac23f39d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..76a27a752 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..741d95d84 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..2f4f8d78c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..c8203e7ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..4c6ee039a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..4cdc84883 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..dcba0a630 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..b150be73e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..2dd39a294 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..c25e0bc9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..fb8bb87c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..e95ced2ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..9416f8990 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..3e3bd1019 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..c947c3b01 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..5a0fd514d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4695f0968 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..80d42215a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..f1d4296e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..042a11434 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..b3645fb6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..4cdf0ec3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..3f9438947 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..417f23ab0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..36bccba59 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..4b085fa13 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..6a4eb0456 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..8b2f9c4f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..8a2cadf70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..36c17e164 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..a0123db05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..c7de2014e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..b3dd053a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..26939c865 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..b9e9b0025 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..bf6f1aacf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..7029cd3a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..65d26569f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..321a7bfa2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..0f51a0f23 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..22f786f19 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..c94181441 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..9ef51409f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..3a22da223 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..d91cd7a52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..4a5d565f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..d8eccbe0f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..305626964 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..11bc00bed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..fef5f953d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..47a84a1ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..68a491c50 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..8e6078fe8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..8ae1261f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..df141baab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..f81e0e6d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..91436d8b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..4bb37af37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..a11c4a804 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..e43048778 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..a3c68908f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..6a0904d35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..d48168505 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..5978bc1a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..24024d5a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..bcb9885fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..ee1b0c2d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..0f28fd863 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..6a880e7a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..b92cd6d78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..0a53e6407 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..17940c4eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..87c099a56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..3133faaa9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..fd303e87d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..76a4c6239 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..370df6193 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..35ad4cc28 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..3df91920b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..678ba09bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..07f2e6c7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..cc1b08b12 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..077165934 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..1c24b0208 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..368f9c29a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..079dff1e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..47742e93b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..fb162e0e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..b020d1b2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..da7ef077f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..3d9e6acb4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..f1f81de34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..d210bbd3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..0813e7887 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..ab54444f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..471e6a052 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..064bbde8e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..8a3676304 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..0587d913b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..1902496f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..c31725531 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..4f8208357 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..e5530e93d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..ecbd753e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..10dee84f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..81c3307e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..92dca0878 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..1df2f9800 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..0ced422cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..d180d92ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..a52536274 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..1e385f7a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..956da8a05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..91f8683e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..88dfc2ef1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..a801238c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..cb0ad174a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..6416020d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..09f9683bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..bf768591b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..b286a9cf2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..e601e3ba4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..0c8889dca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..b9ed3bb6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..3d45dd299 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..1b2141f4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..73711e616 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..1ad61ee39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4c14591f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..6bfde573f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..15f4d43e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..438982d12 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..6aa803b49 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..981d831be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..a5c1b5e0f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..9ddeb45d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..dbf0545cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..8fb04591d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..02d71c43b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..4df2ed5d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..ac4117d58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..6d1454592 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..64e0f4ffb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..921ce46a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4155fe580 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..95c1f4532 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..950785ab1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..eff7333d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..45c85baaa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..87ed9f1a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..66097a6bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..369a2a59f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..62444777d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..390a1828d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..cf75127ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..25264c6f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..441bd2150 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..993437351 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..402fb629e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..2ce50788e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..ab71f8214 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..9f08e39f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..949991675 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..ebbfd50a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..3566f7b87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..000514bdb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..0cccfed6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..e45fde199 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4d8454762 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..f537a705a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..bb603952a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..5c57ca976 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..77bca0c53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..0dc0afb4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..b4587dc87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..a79a99067 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..30771cd35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..38f4fddd9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..7a65961a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..6739059b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..50a80936c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..0291e2d34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..508c1b9b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..6a9d621f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..96309c58c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..d6c3c6b91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..783904646 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..d6fa514b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..5c69a636c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..fb7ae50a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..8e4b55699 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..e929a1a2c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..32d97ce51 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..99365958c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..9c8a00407 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..918563c1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..7031dfea5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..90710df66 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..25df2d7c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..097a20fa3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..e7da5fd5a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..f168da42a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..1b0e9ad74 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..b8616de2c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..a1004da1f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..fcf29ed02 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..2c1110879 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..2c0c44ef3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..c6c04d5ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,47 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..c00018f6c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,47 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..e4fbd97cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..68fb1f7fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..f916f5caa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,47 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..88b7270ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,47 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..68ded3a71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..035999c17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..cf1f54750 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..c6f03b8bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..bc50b5a44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..370b054c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..66433e687 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..37db00353 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..84644d029 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..ad8c2d003 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..333ec8d7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..4edcbbcd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..16b03f7ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..34ece102d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..7a505f51a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..3bfcad1c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..a5d44ce34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..2cb2c7285 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..ba0d60956 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..22a24e57e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..5db7cd825 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..d16f6a0fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..cad496401 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..689b0bcef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..14a72b5e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..a17f62639 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..335d43d57 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..159bf8f52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..54e4a2869 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..35bb2fc4a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..be1e8f22e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..575f6f7ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..764a28169 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..6cec3a872 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4ef9d45b3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..c2e32ca32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..693cef168 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..a184d3cd3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..cfbcb6433 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..040be8c71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..cb19f450a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..72303be7a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..e04ef6199 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..1bd06ec39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..2eaabcf87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..f54a64752 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..781e5914d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..7a5f6fb8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..165326fcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..0a25c7d07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..6059dae7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..df5478418 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..e3832ff2b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..e7328692b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..d65f0231d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..4875d4dd7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..e5a562f72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..5f14734c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..9fcc34917 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..78dc0b5eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..873c6589c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..e6a732b14 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..927f33ba4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..ec19111d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..ad7f5a554 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..07720acf0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..12e87196d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..e8fd53b3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..f5959487f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..172516fcc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..ea87e252f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..1eee39353 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..9a53450a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..8383372d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..f74943222 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..0139bc044 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..76c0a8be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..64d1193b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..7c7373ba0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..643862acf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..4ffed0998 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..7d2a4d6d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..a9eb04aaa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..13b180492 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..3c09a4905 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..c38c42350 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..37d8be574 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..46f72ba22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..db7e48dec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..2ff6a2dc3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..c45c68281 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..96fb25401 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..83dc5bc4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..6a63ad54f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..30295cedf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..66fa835ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..6ba81f2d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..59c0ae5b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..655eb48a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..0952015b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..b3f3b5d3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..c1d37ac71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..c5a4ada93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..c8a57e983 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..4e58d70a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..efd18fc2d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..cfd800946 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..f6a509ab6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..9521a7a25 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..859417475 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..0973a72a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..c6481db2d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..048ef5d44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..a5ea42ed9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..bea2c6cb6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..cb135d383 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..dfcc0df7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..948423a2b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..f9e599079 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..a4f78fb1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..55b433798 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..65a48e212 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..518e81bb4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..398668c59 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..1dd535a1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..4280d885b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..f2dde0200 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..d1e1f8237 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..aae28270d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..6377720c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..5b7cbdc21 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..8ac914b0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..e5f17c585 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..5d20a77fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..246c1cbe6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..0c3cb9262 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..e5219654a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..c98800818 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..592f8eae2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..29bbf58c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..4d467d028 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..3a912b677 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..318e192b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..86d95428f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..e7660cf0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..90fc4a415 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,57 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..3ca42a4e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..d5f356e1b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..ea3610148 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..941f6f160 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..fc4c6a954 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..013bd9dc1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..ca3fee9dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..e7607d630 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..b2bddb0c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..a73dd8079 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..555c361e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..4d8342243 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..361dc7fc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..7ce7cccf0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..67fb188f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..828f7737e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..9761cf1fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..0920a2c97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..80ed9f2fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..8141ac520 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..77403eedf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..e887cccad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..5baf05fb7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..678c57b6f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..1dcf7a4ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..51c03bc8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..06b36c894 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..ccf6af10d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..731ff0522 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..82796e749 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..9d45f6e69 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..97836a8eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..d325592f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..633b88dc1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..25f8cbdb2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..2878988cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..cfc7a5740 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..37b28d3d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..f24a4abfb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..fded221a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4dc449677 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..0abfd1a8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..6d989cb00 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..4aa25874d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..203880f3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..5d3e805dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..cc8663e70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..dfb4cb806 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..0c70c4dec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..df2c5d2b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..dc8590ff0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..21a015ac1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + short shortValue when shortValue < byte.MaxValue => new MyTestId((byte)shortValue), + int intValue when intValue < byte.MaxValue => new MyTestId((byte)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((byte)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..a042941e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..b9bf3c423 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..b182bb279 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..487fe645f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) + { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + } + + public override MyTestId Parse(object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + byte byteValue => new MyTestId(byteValue), + int intValue when intValue < byte.MaxValue => new MyTestId((short)intValue), + long longValue when longValue < byte.MaxValue => new MyTestId((short)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..4f4c265d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..6b8135a9b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..66fec4609 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..38109c7c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + byte byteValue => new MyTestId(byteValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(byte)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..8d8c21d0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..e529d2db9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..b319048ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..8ee2725ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(short)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=0.verified.txt new file mode 100644 index 000000000..7a7d5d2c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=0.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=2.verified.txt new file mode 100644 index 000000000..6df5e56ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=2.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=4.verified.txt new file mode 100644 index 000000000..6f6bad87f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=4.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=6.verified.txt new file mode 100644 index 000000000..e5b6730bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Byte_implementations=6.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public byte Value { get; } + + public MyTestId(byte value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetByte()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=0.verified.txt new file mode 100644 index 000000000..7297cb02e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=0.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=2.verified.txt new file mode 100644 index 000000000..b8c7528f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=2.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=4.verified.txt new file mode 100644 index 000000000..25b9be83e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=4.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=6.verified.txt new file mode 100644 index 000000000..f97fc54ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=Short_implementations=6.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public short Value { get; } + + public MyTestId(short value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt16()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + }